许多子图的 Matplotlib 全局图例 [英] Matplotlib global legend for lots of subplots

查看:48
本文介绍了许多子图的 Matplotlib 全局图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何强制 matplotlib 在我想要的地方添加图例时遇到了一些问题.

i have some problem figuring out how to force matplotlib to add a legend where i want.

我目前所拥有的数字如下所示:

The figures i currently have look like this:

我使用以下脚本生成这2页的图:

I generate this 2 paged plot with the following script:

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.

我可以用 if line == 0 bla bla 来做到这一点,但问题是你可以在我附上的图中看到,例如最后一个图只包含蓝色数据,因此只有蓝色的图例将会显示.我想将完整的图例(红色和蓝色)也放置在同一位置.我什至不知道蓝色和红色的情节会放在什么地方.

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. 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.

这可能吗?

推荐答案

尝试使用 数字图例,您可以为在其中绘制的任何线条创建图例 您的图形并将其相对于图形定位.

Try using a figure legend which will let you create a legend for any of the lines plotted in your figure and position it relative to the figure.

这篇关于许多子图的 Matplotlib 全局图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆