0

we are trying with the below code to get the pie charts but we can only see the percentage in pie chart for each category and unable to print its exact values along with percentage.

dfinfoofbuss = pd.read_csv("file.csv", header=None)

dfinfoofbuss.columns = ['ID', 'INFO']

dfinfoofbuss['VALUE_INFO'] = dfinfoofbuss['ID'].str.split('_').str[1]  

dfinfoofbusscnt = dfinfoofbuss.groupby(['VALUE_INFO']).size().reset_index(name='COUNT_INFO')    

print("dfinfoofbusscnt:",dfinfoofbusscnt)   

plotvar3 = dfinfoofbusscnt.groupby(['VALUE_INFO']).sum().plot(kind='pie' ,title='pie chart', figsize=(6,6), autopct='%.2f', legend = False, use_index=False, subplots=True, colormap="Pastel1")

fig3 = plotvar3[0].get_figure()

fig3.savefig("Info.jpg")    

Sample Data

VALUE_INFO              CountInfo
     abc                     1
     defair                  2
     cdf                    109
     aggr                    1
     sum                     1
     normal                  2
     dev                     1

Is there a way to print its original values along with percentage in pie chart.. Pls suggest

Ravi
  • 793
  • 3
  • 16
  • 29

1 Answers1

1

You will likely need to write your own custom function to get both value and percentage as labels.

Try:

def formatter(x):
    return f"{total*x/100:.0f} ({x:.2f})%"

total = sum(dfinfoofbusscnt["CountInfo"])
plotdata = dfinfoofbusscnt.groupby("VALUE_INFO").sum()
>>> plotdata.plot(kind='pie', 
                  title='pie chart',
                  figsize=(6,6),
                  autopct=formatter,
                  colormap="Pastel1",
                  legend=False,
                  subplots=True
                  )
not_speshal
  • 22,093
  • 2
  • 15
  • 30