Pandas stacked board with grouped bars

I have data that looks like this:

   topic  positive  negative     type
0     88  0.080000  0.030000   source
1     36  0.010000  0.200000   source
2    101  0.350000  0.040000   source
3     78  0.110000  0.090000   source
4     99  0.110000  0.010000   source
5     79  0.000000  0.050000   source
6     24  0.000000  0.160000   source
7     17  0.000000  0.410000   source
8     14  0.090000  0.050000   source
9     29  0.060000  0.030000   source
0     14  0.207071  0.085859  summary
1     17  0.000000  0.738889  summary
2     24  0.000000  0.219349  summary
3     29  0.000000  0.094907  summary
4     36  0.000000  0.255808  summary
5     78  0.108333  0.194444  summary
6     79  0.000000  0.106443  summary
7     88  0.089286  0.041667  summary
8     99  0.098496  0.050877  summary
9    101  0.444444  0.055556  summary

      

I need to draw a dash graph that compares the positive / negative values ​​for different type

for each topic

. I see this as a stacked (positive / negative) barcode with topic

the x-axis and the columns are grouped using the column type

. But I couldn't find a way to plot both the grouped and complex graph.

For one type it looks like this (sorry, I don't have enough reputation to post images):

polar_data.set_index(['type', 'topic']).xs('summary').plot(kind='bar', stacked=True)

      

And the only way I can currently compare the two different types is by simply placing the two parcels side by side with the aid seaborn.factorplot

, which makes it difficult to see trends clearly. And also I don't know how to build a complex graph with seaborn

.

print_data = pd.melt(polar_data, id_vars=['topic', 'type'], value_name='percent', var_name='polarity')
sns.factorplot("topic", 'percent', 'polarity', row="type", data=print_data, margin_titles=True, kind='bar')

      

So, is there a way to "concatenate" them instead of placing them side by side?

+3


source to share


1 answer


Here's one way to do it with matplotlib. I am assuming that the sea wave will use the same structure.

In [3]: polar_data.pivot('topic', 'type')
Out[3]:
       positive            negative
type     source   summary    source   summary
topic
14         0.09  0.207071      0.05  0.085859
17         0.00  0.000000      0.41  0.738889
24         0.00  0.000000      0.16  0.219349
29         0.06  0.000000      0.03  0.094907
36         0.01  0.000000      0.20  0.255808
78         0.11  0.108333      0.09  0.194444
79         0.00  0.000000      0.05  0.106443
88         0.08  0.089286      0.03  0.041667
99         0.11  0.098496      0.01  0.050877
101        0.35  0.444444      0.04  0.055556

      

So now for positive values, you can do -

polar_data.pivot('topic', 'type')['positive'].plot(kind='bar', stacked=True)

      



enter image description here

For negative values, you can do -

polar_data.pivot('topic', 'type')['negative'].plot(kind='bar', stacked=True)

      

enter image description here

+5


source







All Articles