matplotlib - Python Pandas Pivot tables to pie chart -


what best way change non 0 values in following pivot table pie chart using percentages of total(sum of data in pivot table)?

import pandas pd import matplotlib.pyplot plt  df=pd.dataframe({'axis1': ['unix','window','apple','linux'],                  'a': [3,0,1,10],                  'b': [1,0,0,0],                  'c': [0,30,0,20],                  'd': [1,0,12,0],                  }).set_index(['axis1']) 

output:

>>> df           b   c   d axis1                 unix     3  1   0   1 window   0  0  30   0 apple    1  0   0  12 linux   10  0  20   0  [4 rows x 4 columns] 

so want create pie chart pivot table values present , use labels such 'unix a' value of 3/sum=% of in table.

you can plot pie charts matplotlib (eg see http://matplotlib.org/1.2.1/examples/pylab_examples/pie_demo.html).

first stack data (so different columns become index level, see stack docs), , select positive ones:

in [13]: s = df.stack()  in [14]: s = s[s>0]  in [15]: s out[15]:  axis1     unix        3         b     1         d     1 window  c    30 apple       1         d    12 linux      10         c    20 dtype: int64 

then can plot matplotlib pie (for labels, paste 2 levels of index in list comprehension):

in [16]: fig, ax = plt.subplots()     ...: ax.pie(s, labels=["{0} {1}".format(l1, l2) l1, l2 in s.index],      ...:        autopct='%1.1f%%') 

leading output: enter image description here


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -