简体   繁体   English

Matplotlib:在堆积的饼图中放大,填充和居中图例

[英]Matplotlib: enlarge, populate, and center legend in stacked up pie charts

I have a code block which produces vertically aligned pie charts: 我有一个代码块,可以生成垂直对齐的饼图:

import matplotlib.pyplot as plt
import numpy

labels=['Ph 1', 'Ph 2','Ph 3']
colors = ['darkred', 'gold', 'green']
explode = (0.1, 0.1, 0.1)
event1=numpy.array([93.90,0.45,5.65])
event2=numpy.array([82.96,0.86,16.17])
event3=numpy.array([69.25,1.20,29.55])
fig1, (ax1,ax2,ax3)=plt.subplots(3,1,subplot_kw={'aspect':'equal'})
ax2.legend(labels, loc=(-0.05, 0.05), shadow=True)
ax1.pie(event1, explode=explode, colors=colors)
ax2.pie(event2, explode=explode, colors=colors)
ax3.pie(event3, explode=explode, colors=colors)
plt.show()

I want the legend to be aligned to the central pie, but to be placed to its left. 我希望图例与中心饼对齐,但要放在其左侧。 As of now, I get a very small legend which is empty , unreadable , and in the wrong location . 到目前为止,我得到了一个非常小的图例,该图例是空的不可读的并且位于错误的位置 How to fix that? 如何解决? 在此处输入图片说明

How about using patches as follows: 如何使用补丁如下:

import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy

labels= ['Ph 1', 'Ph 2', 'Ph 3']
colors = ['darkred', 'gold', 'green']
explode = (0.1, 0.1, 0.1)
event1=numpy.array([93.90,0.45,5.65])
event2=numpy.array([82.96,0.86,16.17])
event3=numpy.array([69.25,1.20,29.55])
fig1, (ax1,ax2,ax3) = plt.subplots(3,1,subplot_kw={'aspect':'equal'})

ph1 = mpatches.Patch(color='darkred', label='Ph 1')
ph2 = mpatches.Patch(color='gold', label='Ph 2')
ph3 = mpatches.Patch(color='green', label='Ph 3')

ax2.legend(handles=[ph1, ph2, ph3], fontsize=15, loc=(-1.05, 0.15), shadow=True)

ax1.pie(event1, explode=explode, colors=colors)
ax2.pie(event2, explode=explode, colors=colors)
ax3.pie(event3, explode=explode, colors=colors)
plt.show()

This would display as follows: 这将显示如下: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM