Python 三级饼图matplotlib-如何;美化;

Python 三级饼图matplotlib-如何;美化;,python,matplotlib,pie-chart,donut-chart,Python,Matplotlib,Pie Chart,Donut Chart,我试图在matplotlib中模拟一个三层饼图,但对它的美学不满意 具体来说,我似乎无法使这三个级别(或甜甜圈)更具特色。此外,我希望理想情况下将标签显示为图例,而不是它们当前的显示方式。最后,如果可能的话,我希望有一套8色的互补色 我的代码如下: import matplotlib.pyplot as plt import numpy as np first_labels = ["B", "S", "D", "SG", "OBGL", "G", 'T', "O", "I"] first_s

我试图在matplotlib中模拟一个三层饼图,但对它的美学不满意

具体来说,我似乎无法使这三个级别(或甜甜圈)更具特色。此外,我希望理想情况下将标签显示为图例,而不是它们当前的显示方式。最后,如果可能的话,我希望有一套8色的互补色

我的代码如下:

import matplotlib.pyplot as plt
import numpy as np

first_labels = ["B", "S", "D", "SG", "OBGL", "G", 'T', "O", "I"]
first_sizes = [2000, 300, 200, 100, 100, 150, 40, 30, 700]

second_sizes = [1000, 200, 200, 400, 500, 40, 1, 1, 1000]

third_sizes = [500, 300, 400, 500, 400, 100, 5, 2, 800]

flatui = (sns.diverging_palette(20, 250, n=8))


bigger = plt.pie(first_sizes, labels=first_labels, colors=flatui,
                 startangle=90, frame=True, radius = 1)


smaller = plt.pie(second_sizes,
                  colors=flatui, radius=0.9,
                  startangle=90, labeldistance=0.9)



smallest = plt.pie(third_sizes,
                  colors=flatui, radius=0.8,
                  startangle=90, labeldistance=0.8)


centre_circle = plt.Circle((0, 0), 0.7, color='white', linewidth=0)


fig = plt.gcf()
fig.gca().add_artist(centre_circle)

plt.axis('equal')
plt.tight_layout()

plt.show()
有人能建议如何“美化”饼图吗

从和:

您可以通过将名为
wedgeprops
的字典输入到您的馅饼中来定制楔形。例如:

[...] repeating your code [...]

bigger = plt.pie(first_sizes, labels=first_labels, colors=flatui,
                 startangle=90, frame=True, radius = 1,
                wedgeprops={'edgecolor':'k'})


smaller = plt.pie(second_sizes,
                  colors=flatui, radius=0.9,
                  startangle=90, labeldistance=0.9,
                 wedgeprops={'edgecolor':'k'})



smallest = plt.pie(third_sizes,
                  colors=flatui, radius=0.8,
                  startangle=90, labeldistance=0.8,
                  wedgeprops={'edgecolor':'k'})

centre_circle = plt.Circle((0, 0), 0.7, color='white', linewidth=0)

fig = plt.gcf()
fig.gca().add_artist(centre_circle)

# add legend to current ax:
plt.gca().legend(loc='center right', bbox_to_anchor=(1,0,0.5,0.5))

如果希望标签仅在图例中,而不在绘图上:

# make first pie without labels:
bigger = plt.pie(first_sizes, colors=flatui,
                 startangle=90, frame=True, radius = 1,
                wedgeprops={'edgecolor':'k'})

# feed labels to legend:
plt.gca().legend(first_labels, loc='center right', bbox_to_anchor=(1,0,0.5,0.5))

结果很好!
# make first pie without labels:
bigger = plt.pie(first_sizes, colors=flatui,
                 startangle=90, frame=True, radius = 1,
                wedgeprops={'edgecolor':'k'})

# feed labels to legend:
plt.gca().legend(first_labels, loc='center right', bbox_to_anchor=(1,0,0.5,0.5))