Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create multiple pie chart using matplotlib

I have a Pandas DataFrame seems like this

Year    EventCode   CityName    EventCount
2015    10          Jakarta     12
2015    10          Yogjakarta  15
2015    10          Padang      27
...
2015    13          Jayapura    34 
2015    14          Jakarta     24
2015    14          Yogjaarta   15
...
2019    14          Jayapura    12  

i want to visualize top 5 city that have the biggest EventCount (with pie chart), group by eventcode in every year

How can i do that?

like image 398
Pundun Avatar asked May 11 '26 23:05

Pundun


1 Answers

This could be achieved by restructuring your data with pivot_table, filtering on top cities using sort_values and the DataFrame.plot.pie method with subplots parameter:

# Pivot your data
df_piv = df.pivot_table(index='EventCode', columns='CityName',
                        values='EventCount', aggfunc='sum', fill_value=0)


# Get top 5 cities by total EventCount
plot_cities = df_piv.sum().sort_values(ascending=False).head(5).index

# Plot
df_piv.reindex(columns=plot_cities).plot.pie(subplots=True,
                                             figsize=(10, 7),
                                             layout=(-1, 3))

[out]

enter image description here

like image 175
Chris Adams Avatar answered May 14 '26 14:05

Chris Adams