python绘制饼状图图例_使用matplotlib的所有饼图的通用图例

图例只需调用一次,否则将显示7个不同的图例。我在下面展示了一个例子。请注意,您必须将自己的数据替换为ax.pie():data1 = (10,90) # some data to be plotted

data2 = (40,50)

data3 = (70,30)

labels = ['Sending Data', 'Not Sending Data'] #legend labels to be plotted

colors = ['green', 'red']

fig = plt.figure(figsize=(16,8))

ax1 = plt.subplot2grid((2,4),(0,0))

ax1.pie(data1, colors=colors, startangle=90)

plt.title('LOGS1')

ax2 = plt.subplot2grid((2, 4), (0, 1))

ax2.pie(data2, colors=colors, startangle=90)

plt.title('LOGS2')

ax3 = plt.subplot2grid((2, 4), (0, 2))

ax3.pie(data3, colors=colors, startangle=90)

plt.title('LOGS3')

ax4 = plt.subplot2grid((2, 4), (0, 3))

ax4.pie(data1, colors=colors, startangle=90)

plt.title('LOGS4')

ax5 = plt.subplot2grid((2, 4), (1, 0))

ax5.pie(data2, colors=colors, startangle=90)

plt.title('LOGS5')

ax6 = plt.subplot2grid((2, 4), (1, 1))

ax6.pie(data3, colors=colors, startangle=90)

plt.title('LOGS6')

ax7 = plt.subplot2grid((2, 4), (1, 2))

patches, texts = ax7.pie(data1, colors=colors, startangle=90) #use this plot to show the legend

plt.title('LOGS7')

plt.legend(patches, labels, bbox_to_anchor=(2.3, 2), prop={'size':14}) #show the legend defined in labels

#change values of 'bbox_to_anchor' to move the legend to the desired location

plt.axis('equal') # Set aspect ratio to be equal so that pie is drawn as a circle.

plt.tight_layout()

plt.subplots_adjust(right=0.94) #adjust the spacing on right to see legend clearly

plt.show()

这将生成以下图形:

J4up6.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值