Search code examples
pythonfor-loopmatplotlibseabornpie-chart

How do you populate a ndarray figure with pie charts in matplotlib/seaborn?


I have streaming data for genres grouped by country that looks like:

   country             type     value                 count     
198 Canada             Movie    International Movies    141   
199 Canada             Movie    Dramas                  106   
200 Canada             Movie    Comedies                 70   
201 Canada             Movie    Documentaries            48   
202 Canada             Movie    Action & Adventure       41

The example data can be found here: https://rentry.co/kdqy2

I'm trying to create pie charts for the top 10 countries and display them as 2 rows x 5 columns. After reading a few stack overflow solutions listed here and here saying that using a for loop must be modified for ndarrays for it to traverse and populate the plots correctly, I came up with the following code:

fig, axs = plt.subplots(2, 5, figsize=(3*5, 6), subplot_kw={'aspect':'equal'})

for row in axs:
    for ax, (name, sub_df) in zip(row, genre_df.groupby('country')):
        ax.pie(sub_df['count'], labels=sub_df['value'])
        ax.set_title(name)

But this only displays the first five countries twice:

enter image description here

I think I need to associate an index with ax in axs in order to force it onto the second 5 countries instead, but I don't know how to use an enumerate() and a zip() in a for loop simultaneously.


Solution

  • As demonstrated here (and I'm sure elsewhere), you can use ravel() or flatten() or flat and just one loop:

    for ax, (name, sub_df) in zip(axs.ravel(), genre_df.groupby('country')):