gpt4 book ai didi

python - Matplotlib:在堆叠饼图中放大、填充图例并将其居中

转载 作者:太空宇宙 更新时间:2023-11-03 17:12:34 25 4
gpt4 key购买 nike

我有一个代码块,可以生成垂直对齐的饼图:

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()

我希望图例与中央饼图对齐,但放置在其左侧。截至目前,我得到一个非常小的图例,它不可读并且位置错误。如何解决这个问题? enter image description here

最佳答案

如何使用补丁,如下所示:

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()

这将显示如下: enter image description here

关于python - Matplotlib:在堆叠饼图中放大、填充图例并将其居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33953882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com