import matplotlib.pyplot as plt
outer_sizes = [5, 17, 20, 25, 33]
inner_sizes = [1, 4, 5, 12, 6, 14, 4, 21, 9, 24]
outer_colors = ['#499894', '#B07AA1', '#59A14F', '#F28E2B', '#4E79A7']
inner_colors = ['white', '#86BCB6', 'white', '#D4A6C8', 'white', '#8CD17D', 'white', '#FFBE7D',
'white', '#A0CBE8']
plt.pie(outer_sizes,colors=outer_colors, startangle=90,frame=True, radius=4)
plt.pie(inner_sizes,colors=inner_colors,radius=3,startangle=90,autopct='%1.0f%%',
pctdistance=0.8, textprops={'size':22})
center_circle = plt.Circle((0,0), 2, color='black', fc='white', linewidth=0)
fig = plt.gcf()
fig.gca().add_artist(center_circle)
fig.set_size_inches(12,12)
plt.axis('equal')
plt.tight_layout()
plt.show()
我想知道是否有一种方法可以仅在彩色甜甜圈的内部甜甜圈上显示百分比标签。换句话说,我不希望白色切片带有任何标签。
答案 0 :(得分:3)
一种方法是只将标签明确地放入标签中。将此行替换为您的代码:
labels = []
for i, e in enumerate(inner_sizes):
if i%2 != 0:
labels.append('{}%'.format(e))
else:
labels.append('')
plt.pie(inner_sizes,colors=inner_colors,radius=3,startangle=90,labeldistance=.8,
pctdistance=0.8, textprops={'size':22}, labels=labels)
答案 1 :(得分:1)
您可以循环显示自动百分比文本,并在背景为白色的地方将其设置为不可见。与其他答案相比,这样做的好处是您不需要伪造标签(并且如果存在实际的标签,它仍然可以使用)。
import matplotlib.pyplot as plt
outer_sizes = [5, 17, 20, 25, 33]
inner_sizes = [1, 4, 5, 12, 6, 14, 4, 21, 9, 24]
outer_colors = ['#499894', '#B07AA1', '#59A14F', '#F28E2B', '#4E79A7']
inner_colors = ['white', '#86BCB6', 'white', '#D4A6C8', 'white', '#8CD17D', 'white', '#FFBE7D',
'white', '#A0CBE8']
plt.pie(outer_sizes,colors=outer_colors, startangle=90,frame=True, radius=4)
_, _, autopct = plt.pie(inner_sizes,colors=inner_colors,radius=3,startangle=90,autopct='%1.0f%%',
pctdistance=0.8, textprops={'size':22})
for txt, c in zip(autopct, inner_colors):
if c == "white":
txt.set_visible(False)
center_circle = plt.Circle((0,0), 2, color='black', fc='white', linewidth=0)
plt.gca().add_artist(center_circle)
plt.gcf().set_size_inches(12,12)
plt.axis('equal')
plt.tight_layout()
plt.show()