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

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

此问题已在此处有答案

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列的,至少据我所知)

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. [![enter image description here][2]][2]
  4. r2x =np.arange(1,10,1)
  5. rmx =np.arange(1,10,1)
  6. a0_r2_gp = np.random.rand(9)
  7. a0_r2_mf = np.random.rand(9)
  8. a0_r2_mo = np.random.rand(9)
  9. a0_rmse_gp = np.random.randint(0, 60, 9)
  10. a0_rmse_mf = np.random.randint(0, 50, 9)
  11. a0_rmse_mo = np.random.randint(0, 40, 9)
  12. a1_r2_gp = np.random.rand(9)
  13. a1_r2_mf = np.random.rand(9)
  14. a1_r2_mo = np.random.rand(9)
  15. a1_rmse_gp = np.random.randint(0, 30, 9)
  16. a1_rmse_mf =np.random.randint(0, 20, 9)
  17. a1_rmse_mo =np.random.randint(0, 15, 9)
  18. fig, axs = plt.subplots(2, 2, figsize=(8, 12) )
  19. axs[0,0].plot(r2x, np.asarray(a0_r2_gp), marker='o', c = gp_color, label='GP')
  20. axs[0,0].plot(r2x, np.asarray(a0_r2_mf), marker='o', c = mf_color, label='MF')
  21. axs[0,0].plot(r2x, np.asarray(a0_r2_mo), marker='o', c = mo_color, label='MO')
  22. axs[0,0].set_xticks(r2x)
  23. axs[0,0].set_yticks(np.linspace(0,1,11))
  24. axs[0,0].set_ylim(0, 1)
  25. axs[0,0].set_ylabel("r\u00b2, 1st case", fontsize=textfs)
  26. axs[0,1].plot(rmx, np.asarray(a0_rmse_gp), marker='o', c = gp_color, label='GP')
  27. axs[0,1].plot(rmx, np.asarray(a0_rmse_mf), marker='o', c = mf_color, label='MF')
  28. axs[0,1].plot(rmx, np.asarray(a0_rmse_mo), marker='o', c = mo_color, label='MO')
  29. axs[0,1].set_yticks(np.arange(10, 65, 5))
  30. axs[0,1].set_xticks(rmx)
  31. axs[0,1].set_ylim(10, 65)
  32. axs[0,1].set_ylabel("%rmse", fontsize=textfs)
  33. fig.suptitle("r\u00b2 and %rmse averages for Tubes", fontsize=textfs)
  34. axs[1,0].plot(r2x, np.asarray(a1_r2_gp), marker='o', c = gp_color, label='GP')
  35. axs[1,0].plot(r2x, np.asarray(a1_r2_mf), marker='o', c = mf_color, label='MF')
  36. axs[1,0].plot(r2x, np.asarray(a1_r2_mo), marker='o', c = mo_color, label='MO')
  37. axs[1,0].set_xticks(r2x)
  38. axs[1,0].set_yticks(np.linspace(0,1,11))
  39. axs[1,0].set_ylim(0, 1)
  40. axs[1,0].set_ylabel("r\u00b2, 2nd case", fontsize=textfs)
  41. axs[1,1].plot(rmx, np.asarray(a1_rmse_gp), marker='o', c = gp_color, label='GP')
  42. axs[1,1].plot(rmx, np.asarray(a1_rmse_mf), marker='o', c = mf_color, label='MF')
  43. axs[1,1].plot(rmx, np.asarray(a1_rmse_mo), marker='o', c = mo_color, label='MO')
  44. axs[1,1].set_yticks(np.arange(6, 30, 2))
  45. axs[1,1].set_xticks(rmx)
  46. axs[1,1].set_ylim(6, 30)
  47. axs[1,1].set_ylabel("%rmse", fontsize=textfs)
  48. plt.legend(bbox_to_anchor=(0.5, 0), loc="lower center", bbox_transform=fig.transFigure, ncol=3)
  49. plt.tight_layout()
  50. 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

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. r2x =np.arange(1,10,1)
  4. rmx =np.arange(1,10,1)
  5. a0_r2_gp = np.random.rand(9)
  6. a0_r2_mf = np.random.rand(9)
  7. a0_r2_mo = np.random.rand(9)
  8. a0_rmse_gp = np.random.randint(0, 60, 9)
  9. a0_rmse_mf = np.random.randint(0, 50, 9)
  10. a0_rmse_mo = np.random.randint(0, 40, 9)
  11. a1_r2_gp = np.random.rand(9)
  12. a1_r2_mf = np.random.rand(9)
  13. a1_r2_mo = np.random.rand(9)
  14. a1_rmse_gp = np.random.randint(0, 30, 9)
  15. a1_rmse_mf =np.random.randint(0, 20, 9)
  16. a1_rmse_mo =np.random.randint(0, 15, 9)
  17. fig, axs = plt.subplots(2, 2, figsize=(8, 12), layout='constrained' )
  18. axs[0,0].plot(r2x, np.asarray(a0_r2_gp), marker='o', label='GP')
  19. axs[0,0].plot(r2x, np.asarray(a0_r2_mf), marker='o', label='MF')
  20. axs[0,0].plot(r2x, np.asarray(a0_r2_mo), marker='o', label='MO')
  21. axs[0,0].set_xticks(r2x)
  22. axs[0,0].set_yticks(np.linspace(0,1,11))
  23. axs[0,0].set_ylim(0, 1)
  24. axs[0,0].set_ylabel("r\u00b2, 1st case")
  25. axs[0,1].plot(rmx, np.asarray(a0_rmse_gp), marker='o')
  26. axs[0,1].plot(rmx, np.asarray(a0_rmse_mf), marker='o')
  27. axs[0,1].plot(rmx, np.asarray(a0_rmse_mo), marker='o')
  28. axs[0,1].set_yticks(np.arange(10, 65, 5))
  29. axs[0,1].set_xticks(rmx)
  30. axs[0,1].set_ylim(10, 65)
  31. axs[0,1].set_ylabel("%rmse")
  32. fig.suptitle("r\u00b2 and %rmse averages for Tubes")
  33. axs[1,0].plot(r2x, np.asarray(a1_r2_gp), marker='o')
  34. axs[1,0].plot(r2x, np.asarray(a1_r2_mf), marker='o')
  35. axs[1,0].plot(r2x, np.asarray(a1_r2_mo), marker='o')
  36. axs[1,0].set_xticks(r2x)
  37. axs[1,0].set_yticks(np.linspace(0,1,11))
  38. axs[1,0].set_ylim(0, 1)
  39. axs[1,0].set_ylabel("r\u00b2, 2nd case")
  40. axs[1,1].plot(rmx, np.asarray(a1_rmse_gp), marker='o')
  41. axs[1,1].plot(rmx, np.asarray(a1_rmse_mf), marker='o')
  42. axs[1,1].plot(rmx, np.asarray(a1_rmse_mo), marker='o')
  43. axs[1,1].set_yticks(np.arange(6, 30, 2))
  44. axs[1,1].set_xticks(rmx)
  45. axs[1,1].set_ylim(6, 30)
  46. axs[1,1].set_ylabel("%rmse")
  47. fig.legend(loc="outside lower center", ncol=3)
  48. plt.show()

展开查看全部
kxeu7u2r

kxeu7u2r2#

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

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. fig, axs = plt.subplots(2, 2, layout='constrained', sharex=True, sharey=True)
  4. np.random.seed(12345)
  5. for n in range(4):
  6. ax = axs.flat[n]
  7. ll = []
  8. for i in range(3):
  9. l, = ax.plot(np.random.randn(5), label=f'Line {i}')
  10. ll += [l]
  11. fig.legend(handles=ll, loc='outside lower center', ncol=1, fontsize=8, labelspacing=0.5)
  12. plt.show()

展开查看全部

相关问题