为三层甜甜圈添加百分比标签

2024-06-26 18:07:02 发布

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

我想为我的三层油炸圈饼图中的每个标签和图层添加百分比值。你知道吗

下面的代码将适当地生成三层圆环图和图例。但是,它会弄乱百分比值的显示(请参阅本问题底部的输出图)。你知道吗

当前在堆栈溢出或其他地方的解决方案只适用于将百分比值添加到饼图/甜甜圈图(例如:How do I use matplotlib autopct? ),但我的饼图/甜甜圈图有三个层/级别。我的代码如下:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
fig.set_figheight(7)
fig.set_figwidth(22)

ax1 = fig.add_subplot(121)

first_labels = ["Bonjovi", "Superman", "Darius_royale", "Stargazer_1991_viz", "Obligatory_Vis1", "Gertrude", 'Trimbel', "Olio", "Iniwaka"]
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, colors=flatui, startangle=90, frame=True, radius = 1,
                 wedgeprops={'edgecolor':'k'}, autopct = '%1.1%%f')

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

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

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

plt.gca().add_artist(centre_circle)

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

plt.show();

上面代码的结果如下:enter image description here

有人能指导我如何让百分比值整齐地显示在每个甜甜圈圈?你知道吗


Tags: 代码addfigpltfirst百分比colorssizes
1条回答
网友
1楼 · 发布于 2024-06-26 18:07:02

总结我的意见,以下是我从中获得更好图形输出的代码,我相信,您正在寻找的是:

import matplotlib.pyplot as plt
import numpy as np


# Example function that you can call from pie(autopct=autopct)
def autopct(pct):
    if pct > 0.5:
        return f'{pct:.2f}%'
    else:
        return ''


fig = plt.figure()
fig.set_figheight(7)
fig.set_figwidth(22)

ax1 = fig.add_subplot(121)

first_labels = (
    'Bonjovi',
    'Superman',
    'Darius Royale',
    'Stargazer 1991 Viz',
    'Obligatory Vis1',
    'Gertrude',
    'Trimbel',
    'Olio',
    'Iniwaka'
)
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,
    colors=flatui,
    startangle=90,
    frame=True,
    radius=1,
    wedgeprops={'edgecolor':'k'},
#    autopct='%.2f%%',
    autopct=autopct,
    pctdistance=1
)

smaller = plt.pie(
    second_sizes,
    colors=flatui,
    radius=0.9,
    startangle=90,
    wedgeprops={'edgecolor':'k'},
#    autopct='%.2f%%',
    autopct=autopct,
    pctdistance=.9
)

smallest = plt.pie(
    third_sizes,
    colors=flatui,
    radius=0.8,
    startangle=90,
    wedgeprops={'edgecolor':'k'},
#    autopct='%.2f%%',
    autopct=autopct,
    pctdistance=.8
)

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

plt.gca().add_artist(centre_circle)

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

plt.show()

您需要调整pctdistance,直到您对结果满意为止。你知道吗

编辑:

经过一点研究,我写了这个更好的版本:

import matplotlib.pyplot as plt


fig, ax = plt.subplots()
ax.axis('equal')

sizes = dict(
    first = (2000, 300, 200, 100, 100, 150, 40, 30, 700),
    second = (1000, 200, 200, 400, 500, 40, 1, 1, 1000),
    third = (500, 300, 400, 500, 400, 100, 5, 2, 800)
)

percentiles = dict(
    first = [x*100/sum(sizes['first']) for x in sizes['first']],
    second = [x*100/sum(sizes['second']) for x in sizes['second']],
    third = [x*100/sum(sizes['third']) for x in sizes['third']]
)

labels = dict(
    first = [f"{x:.2f}%" if x >.5 else '' for x in percentiles['first']],
    second = [f"{x:.2f}%" if x >.5 else '' for x in percentiles['second']],
    third = [f"{x:.2f}%" if x >.5 else '' for x in percentiles['third']]
)

width = 0.35
radius = 1.5

first, _ = ax.pie(
    sizes['first'],
    startangle=90,
    radius=radius,
    labeldistance=.9,
    labels=labels['first'],
    rotatelabels=True
)

second, _ = ax.pie(
    sizes['second'],
    radius=radius - width,
    startangle=90,
    labeldistance=.9,
    labels=labels['second'],
    rotatelabels=True
)

third, _ = ax.pie(
    sizes['third'],
    radius=radius - 2 * width,
    startangle=90,
    labeldistance=.9,
    labels=labels['third'],
    rotatelabels=True
)

plt.setp(first + second + third, width=width, edgecolor='white')

plt.show()

相关问题 更多 >