Search code examples
pythonmatplotlibpie-chart

Produce pie chart subplots for unique results in groupby?


What is the best way to produce pie charts for unique values in a Dataframe?

I have a DataFrame that shows number of services by county. I would like to produce a group of pie charts for every county that shows the count of services in that county. I've tried a variety of different approaches without much success.

Here's my data

print(mdfgroup)

    County     Service    ServiceCt
0   Alamance    Literacy          1
1   Alamance   Technical          1
2   Alamance  Vocational          4
3    Chatham    Literacy          3
4    Chatham   Technical          2
5    Chatham  Vocational          1
6     Durham    Literacy          1
7     Durham   Technical          1
8     Durham  Vocational          1
9     Orange    Literacy          1
10      Wake    Literacy          2
11      Wake   Technical          2

So there would be one chart for Alamance with slices for literacy, technical, vocational; a chart for Chatham, Durham, etc. Slice size would be based on ServiceCt.

I've been experimenting with a lot of different approaches but I'm not sure what the most efficient would be. I tried but below it doesn't really break it down by county and it's not resulting in any graphs.

for i, row in enumerate(mdfgroup.itertuples(),1):
       plt.figure()
       plt.pie(row.ServiceCt,labels=row.Service,
       startangle=90,frame=True, explode=0.2,radius=3)
plt.show()

This throws an error:

TypeError: len() of unsized object

and then produces a blank plot box

(I can't embed an image yet so here's the link) Blank Plot Box

Ideally I'd like them to all be subplots, but at this stage I'd take a series of individual plots. The other examples I've found don't deal with unique values for a key (County).


Solution

  • Is this what you had in mind?

    Ncounties = len(mdfgroup.County.unique())
    fig, axs = plt.subplots(1, Ncounties, figsize=(3*Ncounties,3), subplot_kw={'aspect':'equal'})
    for ax,(groupname,subdf) in zip(axs,mdfgroup.groupby('County')):
        ax.pie(subdf.ServiceCt, labels=subdf.Service)
        ax.set_title(groupname)
    

    enter image description here