Matplotlib:添加第二个图例以将其伪装为plot ti

2024-09-26 22:50:11 发布

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

我有以下代码可以生成饼图:

#Parameters
labels=['Ph 1', 'Ph 2','Ph 3']
colors = ['darkred', 'gold', 'green']
explode = (0.1, 0.1, 0.1) #Explode the all slices to make them more visible.

ph1=numpy.array([93.90,0.45,5.65]) 
ph2=numpy.array([82.96,0.86,16.17]) 
ph3=numpy.array([69.25,1.20,29.55])
fig1, (ax1,ax2,ax3)=plt.subplots(3,1,subplot_kw={'aspect':'equal'})
fig1.subplots_adjust(bottom=0.3)
ph1 = mpatches.Patch(color='darkred', label='Ph 1')
ph2 = mpatches.Patch(color='gold', label='Ph 2')
ph3 = mpatches.Patch(color='green', label='Ph 3')
ax1.legend
ax2.legend(handles=[ph1, ph2, ph3], fontsize=10, loc=(-1.2, 0.3), prop={'size':10},shadow=True)
ax1.pie(ph1, labels=None, explode=explode, colors=colors, autopct='%.2f')
ax2.pie(ph2, labels=None, explode=explode, colors=colors, autopct='%.2f')
ax3.pie(ph3, labels=None, explode=explode, colors=colors, autopct='%.2f')
ax1.text(1.1,0.4,'Ph 1, A=0.2', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
ax2.text(1.1,0.4,'Ph 2, B=0.3', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
ax3.text(1.1,0.4,'Ph 3, C=0.4', fontsize=8, bbox=dict(facecolor='red', alpha=0.5))
plt.tight_layout()
plt.savefig('fig.jpg',bbox_inches='tight')
plt.close()

如何在绘图区外的左上角插入第二个图例,标题放在哪里?在

这就是我现在所拥有的:

enter image description here


Tags: numpylabelspltarrayphcolorsexplodebbox

热门问题