Home > Software design >  how to plot many categories with matplotlib?
how to plot many categories with matplotlib?

Time:10-29

Consider the example below

dfa = pd.DataFrame({'type' : ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'],
                   'value' : [2,3,4,2,5,3,6,5,3,1,3,5,7,5,3,5,4],
                   'date' : [pd.to_datetime('2021-01-01')]*17})

dfa
Out[337]: 
   type  value       date
0   a    2     2021-01-01
1   b    3     2021-01-01
2   c    4     2021-01-01
3   d    2     2021-01-01
4   e    5     2021-01-01
5   f    3     2021-01-01
6   g    6     2021-01-01
7   h    5     2021-01-01
8   i    3     2021-01-01
9   j    1     2021-01-01
10  k    3     2021-01-01
11  l    5     2021-01-01
12  m    7     2021-01-01
13  n    5     2021-01-01
14  o    3     2021-01-01
15  p    5     2021-01-01
16  q    4     2021-01-01

As you can see, I have (too) many categories but I still need to plot all of them at the same time. I tried to use the hatch argument in matplotlib but this does not seem to shade some patterns and not the others (so that more categories are visually distinct).

dfa.set_index(['date','type']).unstack().plot.bar(stacked = True, hatch = 'o')

enter image description here

What can I do here? Thanks!

CodePudding user response:

You could loop through the generated bars, and assign a unique stack bar plot with individual hatching

  • Related