Python图例旁边的plot和in saved figu

2024-10-01 15:37:49 发布

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

我在整个论坛上搜索,但还没有找到问题的答案。在

我想创建两个相邻的饼图,旁边有一个图例。在

labels = ['0-20', '20-40',  '40-60', '60-80', '80-100', '100-120']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'orange', 'grey']

fig, [ax1, ax2] = plt.subplots(1,2)
ax1.pie(groen, colors = colors, startangle = 90, counterclock = False)
ax1.axis('equal')

ax2.pie(rood, colors = colors, startangle = 90, counterclock = False)
ax2.axis('equal')
plt.legend(labels, loc = 'best')#, bbox_to_anchor=(0.5, 0), mode = 'expand', ncol = 2)

我一直在尝试一些东西,但不幸的是没有结果。出了问题的是,传奇没有完全出现在保存的图像中,如下所示。我还得到了一个白色的酒吧和传说是在错误的顺序。我想从左到右,从上到下读。在

enter image description here

欢迎任何帮助;)


Tags: 答案falselabelspltequal论坛colorspie
1条回答
网友
1楼 · 发布于 2024-10-01 15:37:49

使用以下代码时

import matplotlib.pyplot as plt
groen = rood = [7,8,12,4,9,5]
labels = ['0-20', '20-40',  '40-60', '60-80', '80-100', '100-120']
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue', 'orange', 'grey']

fig, [ax1, ax2] = plt.subplots(1,2)
ax1.pie(groen, colors = colors, startangle = 90, counterclock = False)
ax1.axis('equal')

ax2.pie(rood, colors = colors, startangle = 90, counterclock = False)
ax2.axis('equal')
plt.legend(labels, loc = 8, ncol = 2)

plt.savefig(__file__+".png", bbox_inches="tight")
plt.show()

我想一切都按预期进行。注意bbox_inches="tight"参数,它缩小或扩展图形大小,这样就不会裁剪任何内容。在

enter image description here

相关问题 更多 >

    热门问题