gpt4 book ai didi

python - 三层饼图 matplotlib - 如何 "prettify"

转载 作者:行者123 更新时间:2023-12-01 00:14:26 25 4
gpt4 key购买 nike

我试图在 matplotlib 中模拟一个三层饼图,但对其美观不满意。

enter image description here

具体来说,我似乎无法使三个级别(或 donut )更加独特。另外,我想理想地将标签显示为图例,而不是它们当前的显示方式。最后,如果可能的话,我希望有 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()

有人可以建议如何“美化”饼图吗?

最佳答案

来自 documentation of plt.piethis page :

[...]
wedgeprops : dict, optional, default: None
Dict of arguments passed to the wedge objects making the pie. For example, you can pass in wedgeprops = {'linewidth': 3} to set the
width of the wedge border lines equal to 3. For more details, look at
the doc/arguments of the wedge object. By default clip_on=False.
[...]

您可以通过向饼图提供名为 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))

enter image description here

如果您希望标签仅出现在图例中而不是绘图中:

# 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))

enter image description here

关于python - 三层饼图 matplotlib - 如何 "prettify",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59415361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com