Add source example files
This commit is contained in:
24
source/plotting/charts/pie_chart.py
Normal file
24
source/plotting/charts/pie_chart.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
Display sales of various cities in a sunburst chart.
|
||||
|
||||
Given the cities can have recurring parents, we can have
|
||||
a sunburst chart with 2 rings, the centermost ring for the
|
||||
country, and the outmost ring to have sales per city.
|
||||
"""
|
||||
import matplotlib
|
||||
import pandas as pd
|
||||
from matplotlib import pyplot
|
||||
|
||||
if __name__ == '__main__':
|
||||
matplotlib.use("QtCairo")
|
||||
df = pd.DataFrame(data={
|
||||
"country": ["France", "France", "Spain", "Spain"],
|
||||
"city": ["Montpellier", "Bordeaux", "Madrid", "Valencia"],
|
||||
"sales": [150_000, 127_000, 97_200, 137_250]
|
||||
})
|
||||
df.set_index(["country", "city"], inplace=True)
|
||||
total: int = df["sales"].sum()
|
||||
print(df)
|
||||
axes = df.plot.pie(subplots=True, autopct=lambda x: f"{x * total / 100:.0f} {x:.2f}%")
|
||||
pyplot.show()
|
||||
|
Reference in New Issue
Block a user