I am working on financial data, I intend to figure out how to create a nested pie chart on my data. Specifically, I filtered export and import product data and render nested plot for it. I did render pie chart for each, I am not able to get a correct nested pie chart or donut chart for the data. I looked into possible post on SO
but didn't find any clue how to get my plot.
my current output :
import pandas as pd
from matplotlib import pyplot as plt
df5=df_from_gist_exp.groupby(['cty_ptn'])['qty1'].sum().nlargest(10)
df6=df_from_gist_imp.groupby(['cty_ptn'])['qty1'].sum().nlargest(10)
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.pie(df5, labels=df5.index, autopct='%1.0f%%', radius=1)
ax2.pie(df6, labels=df6.index, autopct='%1.0f%%', radius=1)
plt.axis('equal')
plt.tight_layout()
plt.show()
current plot :
I got this plot after running the above solution:
desired plot
actually I want to render this pie chart or donut chart using the same data:
How can I get this plot? Any trick to make this happen? Thanks
I just made a minimal code to achieve what you wanted:
import matplotlib.pyplot as plt
import numpy as np
# Pie chart, where the slices will be ordered and plotted counter-clockwise:
lbls = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
# Intended to serve something like a global variable
class MyClass:
i = -1
def func(pct, labels, vals):
MyClass.i +=1
# Returns absolute value against the default percentage
# absolute = int(pct/100.*np.sum(vals))
# Combine labels and values
return "{:s}\n{:.0f} %".format(labels[MyClass.i], pct)
fig1, ax1 = plt.subplots()
# Pie wedgeprops with width being the donut thickness
ax1.pie(sizes, wedgeprops=dict(width=0.7), autopct=lambda pct: func(pct, lbls, sizes),
shadow=True, startangle=90)
sumstr = 'Total = '+str(np.sum(sizes))
# String on the donut center
ax1.text(0., 0., sumstr, horizontalalignment='center', verticalalignment='center')
ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
This produces the following chart:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.