0

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.

exlo
  • 315
  • 1
  • 8
  • 20
  • 2
    One loop: `for ax, (name, sub_df) in zip(axs.ravel(), genre_df.groupby('country')):` – BigBen Mar 31 '21 at 20:12
  • 2
    You might consider using a legend instead of all those labels. – BigBen Mar 31 '21 at 20:15
  • Thank you, .ravel() is a far simpler than the other solutions I've come across! If you make it an answer reply, I would select it as the answer for this question. – exlo Mar 31 '21 at 20:45
  • Not quite because I now know what .ravel() is from documentation after you suggested it but did not know to use it in the first place given other SO questions in the same vein. I don't think I would have found this duplicate in my own search since the question already points to .ravel() as a way to flatten the ndarray. I think this question is approaching it at a different angle and could be helpful to others. But if you think it's a duplicate, I will mark it as such. – exlo Mar 31 '21 at 20:57

1 Answers1

1

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')):
BigBen
  • 46,229
  • 7
  • 24
  • 40