[英]Changing position of matplotlib plot legend
I want to change the position of a plot legend relative to the position of the plot.我想改变情节图例相对于情节位置的位置。 Here's the code for the plot:
这是情节的代码:
fig = plt.subplot(122)
legend = []
plt.plot(k_array, NDCG, 'red')
legend.append("utility overall NDCG")
if data_loader.GSQb:
plt.plot(k_array, GSQNDCG, 'orange')
legend.append("GSQ NDCG")
if data_loader.BSQb:
plt.plot(k_array, BSQNDCG, 'purple')
legend.append("BSQ NDCG")
if data_loader.GWQb:
plt.plot(k_array, GWQNDCG, 'black')
legend.append("GWQ NDCG")
fig.legend(legend)
#plt.legend(bbox_to_anchor=(-1, 1),fontsize= 'large')
plt.savefig(metaCat + " best utility NDCGS")
And here's how the resulting PNG looks:这是生成的 PNG 的外观:
If possible, I'd like to make the legend appear in the large space to the left of the plot (preferably with the same size).如果可能,我想让图例出现在情节左侧的大空间中(最好具有相同的大小)。 How can I make this happen?
我怎样才能做到这一点?
Edit: I've read through the docs and Moving matplotlib legend outside of the axis makes it cutoff by the figure box , and I'm still having trouble figuring it out.编辑:我已经阅读了文档,并且在轴外移动 matplotlib 图例使它被图形框截断,但我仍然无法弄清楚。
It's well written in the docs .它在文档中写得很好。 You need to use the
loc
parameter to select the location of the legend.您需要使用
loc
参数来选择图例的位置。
Try to use bbox_to_anchor=(x, y, width, height)
as well (maybe with negative values) to move the legend outside of the figure.尝试使用
bbox_to_anchor=(x, y, width, height)
以及(可能带有负值)将图例移到图形之外。
Yoy may try this你可以试试这个
fig = plt.subplot(122)
legend = []
plt.plot(k_array, NDCG, 'red')
legend.append("utility overall NDCG")
if data_loader.GSQb:
plt.plot(k_array, GSQNDCG, 'orange')
legend.append("GSQ NDCG")
if data_loader.BSQb:
plt.plot(k_array, BSQNDCG, 'purple')
legend.append("BSQ NDCG")
if data_loader.GWQb:
plt.plot(k_array, GWQNDCG, 'black')
legend.append("GWQ NDCG")
fig.legend(legend)
plt.legend(bbox_to_anchor=(-1, 1),fontsize= 'large', loc='upper left')
plt.savefig(metaCat + " best utility NDCGS")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.