饼图大小、标签、距离

2024-05-15 13:23:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个非常好的饼图,但它仍然不是完美的。 我的代码是:

labels_occurancy = ['composition', 'process', 'pathology', 'other_meds', 
'disposal', 'animal', 'device', 'cultivation', 'food', 
'tests', 'compliance']
size_occurancy = [199, 179, 63, 128, 33, 8, 44, 24, 37, 31, 4]
explode = (0.008, 0.008, 0.008,0.008,0.008,0.008,0.008,0.008,0.008,0.008,0.008)
textprops = {"fontsize":15}
plt.pie(size_occurancy, labels=labels_occurancy, explode=explode,
        autopct='%1.1f%%', pctdistance = 0.8, textprops =textprops,
        radius = 0.5)
plt.title('Classification in %', fontdict = {'fontsize':20})
plt.show()

下一个问题是我的图表。按照原来的尺寸,整个屏幕上的馅饼都很大。这就是为什么我决定使用radius。但随着减少,我的标签变得不清晰,可读性降低(即使是为了增加标签的大小)。最大的问题是,随着半径的减小,图表下方和下方仍有大量空间。smb能给我一些解释或建议吗


Tags: 代码sizelabels图表plt标签processother
2条回答

我希望以下代码能帮助您:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))

recipe = ["composition",
          "process",
          "pathology",
          "other_meds",
          "disposal",
          "animal",
          "device",
          "cultivation",
          "food",
          "tests",
          "pathology"]


data = [199, 179, 63, 128, 33, 8, 44, 24, 37, 31, 4]



#When you change the width to 0.5 you get a donut chart

wedges, texts = ax.pie(data, wedgeprops=dict(width=1), startangle=-140)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
          bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1)/2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(recipe[i], xy=(x, y), xytext=(2*np.sign(x), 2*y),
                horizontalalignment=horizontalalignment, **kw)

#ax.set_title("Title of graph")

plt.show()

输出:

enter image description here

您仍然可以将相应的百分比和标题添加到图表中。但我还是不明白你想用爆炸0.008表达什么

正如有时发生的那样,在重新启动计算机后,图形完全变了形,经过小的修改后,我不再有任何空间问题。所有其他图形都正常,它们没有改变任何形状

相关问题 更多 >