[英]Matplotlib global legend for lots of subplots
i have some problem figuring out how to force matplotlib to add a legend where i want. 我在弄清楚如何强制matplotlib在我想要的位置添加图例时遇到一些问题。
The figures i currently have look like this: 我目前的数字如下所示:
I generate this 2 paged plot with the following script: 我使用以下脚本生成此2页的图:
n = 1
trn = True
for i in diff.keys():
plt.subplot(8, 4, n)
plt.xlim([0, rounds + 1])
plt.locator_params(axis='y', nbins=5)
if PEPTIDE_KD[i] == 1e-2:
plt.plot(x_cord, [x[0] for x in diff[i]], 'ro--', label='asd')
plt.plot(x_cord, [x[1] for x in diff[i]], 'rv--', label='bsd')
else:
plt.plot(x_cord, [x[0] for x in diff[i]], 'bo--', label='asd')
plt.plot(x_cord, [x[1] for x in diff[i]], 'bv--', label='csd')
plt.title(i)
n += 1
if n == 33 and trn:
n = 1
trn = False
plt.suptitle('Ligand energies')
plt.locator_params(axis='y', nbins=5)
plt.subplots_adjust(hspace=0.35)
plt.show()
plt.legend(bbox_to_anchor=(-0.05, 1), loc=1)
plt.locator_params(axis='y', nbins=5)
plt.subplots_adjust(hspace=0.35)
plt.show()
However i would like to place the legend at the left side of the very first subplot, or to the right side of last subplot in the first line. 但是我想将图例放在第一个子图的左侧,或放在第一行中最后一个子图的右侧。
I can do this with an if line == 0
bla bla, but the problem is as you can see in the plots i attached that for example the last plot only contains blue data, hence only the blue legend will show. 我可以使用if line == 0
bla bla来执行此操作,但是问题是,正如您在我所附的图中看到的那样,例如最后一个图仅包含蓝色数据,因此仅显示蓝色图例。 I would like to place a full legend (red and blue) as well to the very same place. 我想将完整的图例(红色和蓝色)也放置在同一位置。 I dont even know where blue and red plot will be placed beforehand. 我什至不知道蓝色和红色的情节将被预先放置在哪里。
Is this possible? 这可能吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.