我在一起绘图中有2张图表,我希望图例放在两张图下方的空白处。出于某种原因,它总是在其中一个馅饼的顶部,即使我做“底部”。
另外,我已经两次使用“title”属性来单独标记每个图形。如何移动图表下方的标签? title属性似乎没有“loc”属性。
谢谢!
labels = 'a', 'b', 'c', 'd'
fracsQuads = [6, 14, 1, 79]
fracsTrips = [11, 16, 7, 66]
colors=['Goldenrod', 'LimeGreen', 'Crimson', 'DeepSkyBlue']
explode=(0, 0, 0, 0)
# Make square figures and axes
the_grid = GridSpec(1, 2)
plt.subplot(the_grid[0, 0], aspect=1)
plt.pie(fracsQuads, autopct='%1.0f%%', colors=colors, pctdistance=1.2)
plt.title('Four knockouts')
plt.subplot(the_grid[0, 1], aspect=1)
plt.pie(fracsTrips, explode=explode, autopct='%.0f%%', colors=colors, pctdistance=1.2)
plt.title('Three knockouts')
#plt.legend(labels, loc='best')
font = {'family' : 'normal',
'weight' : 'normal',
'size' : 14}
matplotlib.rc('font', **font)
plt.show()
plt.savefig('pythonFigureTest.png', facecolor='white', transparent=True)
答案 0 :(得分:0)
关键是你没有标记轴。因此,图例将始终相对于右图放置,因为这是最后创建的轴。
您可以使用bbox_to_anchor
手动移动到图例框。此外,我删除了标题,并使用figtext()
手动将文本放在图表下方。
以下代码应该这样做:
font = {'family' : 'normal',
'weight' : 'normal',
'size' : 14}
rcParams.update(font)
labels = 'a', 'b', 'c', 'd'
fracsQuads = [6, 14, 1, 79]
fracsTrips = [11, 16, 7, 66]
colors=['Goldenrod', 'LimeGreen', 'Crimson', 'DeepSkyBlue']
explode=(0, 0, 0, 0)
# Make square figures and axes
the_grid = GridSpec(1, 2)
ax1 = plt.subplot(the_grid[0, 0], aspect=1)
ax2 = plt.subplot(the_grid[0, 1], aspect=1)
ax1.pie(fracsQuads, autopct='%1.0f%%', colors=colors, pctdistance=1.2)
ax2.pie(fracsTrips, explode=explode, autopct='%.0f%%', colors=colors, pctdistance=1.2)
plt.figtext(0.19,0.2,'Four knockouts')
plt.figtext(0.61,0.2,'Three knockouts')
ax1.legend(labels, loc='lower center',bbox_to_anchor=(1.1, -0.1),
prop={'size':11})
plt.show()
我在图例框中使用了小字体,以便在它们之间很好地适应。如果你有更长的文字,你可能不得不摆弄数字。