matplotlib 如何将图例放在2列图定义的图外?[重复]

q3aa0525  于 2023-10-24  发布在  其他
关注(0)|答案(2)|浏览(105)

此问题已在此处有答案

Legend location in matplotlib subplot(1个答案)
How to put the legend outside the plot(18个回答)
How to specify legend position in graph coordinates(5个答案)
上个月关门了。


我试图在2列5行之外绘制图例,但在matplotlib中的这个MWE 2行示例中。结果图例“lower center“也在两个图之间放置了空间,这是不可取的。有人有任何建议来纠正这个问题吗?理想情况下,需要两列之间的默认间距。(已经看过various examples,这是一个单一的情节exmamples,围绕bbox_to_anchor移动,不存在2列的,至少据我所知)

import numpy as np
import matplotlib.pyplot as plt
[![enter image description here][2]][2]
r2x =np.arange(1,10,1)
rmx =np.arange(1,10,1)

a0_r2_gp = np.random.rand(9)
a0_r2_mf = np.random.rand(9)
a0_r2_mo = np.random.rand(9)

a0_rmse_gp = np.random.randint(0, 60, 9)
a0_rmse_mf = np.random.randint(0, 50, 9)
a0_rmse_mo = np.random.randint(0, 40, 9)

a1_r2_gp = np.random.rand(9)
a1_r2_mf = np.random.rand(9)
a1_r2_mo = np.random.rand(9)

a1_rmse_gp = np.random.randint(0, 30, 9)
a1_rmse_mf =np.random.randint(0, 20, 9)
a1_rmse_mo =np.random.randint(0, 15, 9)

fig, axs = plt.subplots(2, 2, figsize=(8, 12) )
axs[0,0].plot(r2x, np.asarray(a0_r2_gp), marker='o', c = gp_color, label='GP')
axs[0,0].plot(r2x, np.asarray(a0_r2_mf), marker='o', c = mf_color, label='MF')
axs[0,0].plot(r2x, np.asarray(a0_r2_mo), marker='o', c = mo_color, label='MO')
axs[0,0].set_xticks(r2x)
axs[0,0].set_yticks(np.linspace(0,1,11))
axs[0,0].set_ylim(0, 1)
axs[0,0].set_ylabel("r\u00b2, 1st case", fontsize=textfs)

axs[0,1].plot(rmx, np.asarray(a0_rmse_gp), marker='o', c = gp_color, label='GP')
axs[0,1].plot(rmx, np.asarray(a0_rmse_mf), marker='o', c = mf_color, label='MF')
axs[0,1].plot(rmx, np.asarray(a0_rmse_mo), marker='o', c = mo_color, label='MO')
axs[0,1].set_yticks(np.arange(10, 65, 5))
axs[0,1].set_xticks(rmx)
axs[0,1].set_ylim(10, 65)
axs[0,1].set_ylabel("%rmse", fontsize=textfs)
fig.suptitle("r\u00b2 and %rmse averages for Tubes", fontsize=textfs)

axs[1,0].plot(r2x, np.asarray(a1_r2_gp), marker='o', c = gp_color, label='GP')
axs[1,0].plot(r2x, np.asarray(a1_r2_mf), marker='o', c = mf_color, label='MF')
axs[1,0].plot(r2x, np.asarray(a1_r2_mo), marker='o', c = mo_color, label='MO')
axs[1,0].set_xticks(r2x)
axs[1,0].set_yticks(np.linspace(0,1,11))
axs[1,0].set_ylim(0, 1)
axs[1,0].set_ylabel("r\u00b2, 2nd case", fontsize=textfs)

axs[1,1].plot(rmx, np.asarray(a1_rmse_gp), marker='o', c = gp_color, label='GP')
axs[1,1].plot(rmx, np.asarray(a1_rmse_mf), marker='o', c = mf_color, label='MF')
axs[1,1].plot(rmx, np.asarray(a1_rmse_mo), marker='o', c = mo_color, label='MO')
axs[1,1].set_yticks(np.arange(6, 30, 2))
axs[1,1].set_xticks(rmx)
axs[1,1].set_ylim(6, 30)
axs[1,1].set_ylabel("%rmse", fontsize=textfs)

plt.legend(bbox_to_anchor=(0.5, 0), loc="lower center", bbox_transform=fig.transFigure, ncol=3)
plt.tight_layout()
plt.show()
dnph8jn4

dnph8jn41#

1.使用constrained layout而不是tight_layout
1.使用图例而不是plt.legend(它是ax.legend的 Package ),并将loc设置为“外下中心”。
1.仅在其中一个轴内设置标签,否则图例条目将重复。
另请参见https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html#figure-legends

import numpy as np
import matplotlib.pyplot as plt

r2x =np.arange(1,10,1)
rmx =np.arange(1,10,1)

a0_r2_gp = np.random.rand(9)
a0_r2_mf = np.random.rand(9)
a0_r2_mo = np.random.rand(9)

a0_rmse_gp = np.random.randint(0, 60, 9)
a0_rmse_mf = np.random.randint(0, 50, 9)
a0_rmse_mo = np.random.randint(0, 40, 9)

a1_r2_gp = np.random.rand(9)
a1_r2_mf = np.random.rand(9)
a1_r2_mo = np.random.rand(9)

a1_rmse_gp = np.random.randint(0, 30, 9)
a1_rmse_mf =np.random.randint(0, 20, 9)
a1_rmse_mo =np.random.randint(0, 15, 9)

fig, axs = plt.subplots(2, 2, figsize=(8, 12), layout='constrained' )
axs[0,0].plot(r2x, np.asarray(a0_r2_gp), marker='o', label='GP')
axs[0,0].plot(r2x, np.asarray(a0_r2_mf), marker='o', label='MF')
axs[0,0].plot(r2x, np.asarray(a0_r2_mo), marker='o', label='MO')
axs[0,0].set_xticks(r2x)
axs[0,0].set_yticks(np.linspace(0,1,11))
axs[0,0].set_ylim(0, 1)
axs[0,0].set_ylabel("r\u00b2, 1st case")

axs[0,1].plot(rmx, np.asarray(a0_rmse_gp), marker='o')
axs[0,1].plot(rmx, np.asarray(a0_rmse_mf), marker='o')
axs[0,1].plot(rmx, np.asarray(a0_rmse_mo), marker='o')
axs[0,1].set_yticks(np.arange(10, 65, 5))
axs[0,1].set_xticks(rmx)
axs[0,1].set_ylim(10, 65)
axs[0,1].set_ylabel("%rmse")
fig.suptitle("r\u00b2 and %rmse averages for Tubes")

axs[1,0].plot(r2x, np.asarray(a1_r2_gp), marker='o')
axs[1,0].plot(r2x, np.asarray(a1_r2_mf), marker='o')
axs[1,0].plot(r2x, np.asarray(a1_r2_mo), marker='o')
axs[1,0].set_xticks(r2x)
axs[1,0].set_yticks(np.linspace(0,1,11))
axs[1,0].set_ylim(0, 1)
axs[1,0].set_ylabel("r\u00b2, 2nd case")

axs[1,1].plot(rmx, np.asarray(a1_rmse_gp), marker='o')
axs[1,1].plot(rmx, np.asarray(a1_rmse_mf), marker='o')
axs[1,1].plot(rmx, np.asarray(a1_rmse_mo), marker='o')
axs[1,1].set_yticks(np.arange(6, 30, 2))
axs[1,1].set_xticks(rmx)
axs[1,1].set_ylim(6, 30)
axs[1,1].set_ylabel("%rmse")

fig.legend(loc="outside lower center", ncol=3)
plt.show()

kxeu7u2r

kxeu7u2r2#

没有必要在现代Matplotlib中使用subplots_adjust:https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html#figure-legends。在layout='constrained'中使用“外部下中心”。
编辑(抱歉,下面与@RuthC交叉发布)。

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2, layout='constrained', sharex=True, sharey=True)
np.random.seed(12345)
for n in range(4):
    ax = axs.flat[n]
    ll = []
    for i in range(3):
        l, = ax.plot(np.random.randn(5), label=f'Line {i}')
        ll += [l]

fig.legend(handles=ll, loc='outside lower center', ncol=1, fontsize=8, labelspacing=0.5)

plt.show()

相关问题