此问题已在此处有答案:
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()
2条答案
按热度按时间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
kxeu7u2r2#
没有必要在现代Matplotlib中使用
subplots_adjust
:https://matplotlib.org/stable/tutorials/intermediate/legend_guide.html#figure-legends。在layout='constrained'
中使用“外部下中心”。编辑(抱歉,下面与@RuthC交叉发布)。