matplotlib 如何调整 legend 的位置

请先 登录 后评论

查看全部 1 个回答

匿名 - 研究生

图例的注释使用方法如下:

plt.legend(loc=’, title='',fontsize=12, frameon=True, fancybox=True, framealpha=0.2, borderpad=0.3, ncol=1, markerfirst=True, markerscale=1, bbox_to_anchor='',numpoints=1, handlelength=3.5,shadow='')

其中:

  • loc 设置图例的位置,可取值:‘best’=0,‘upper right’=1,‘upper left’=2,‘lower left’=3,‘lower right’=4,‘right’=5,‘center left’=6,‘center right’=7,‘lower center’=8,‘upper center’=9,‘center’=10;
  • ncol 设置图例的数量,默认值为 1
  • bbox_to_anchor 设置图例的位置,与 loc 配合使用

例如:

ax.legend(loc='lower center', bbox_to_anchor=(0.5, 1),ncol=3)

图例效果如下:

attachments-2021-08-OfQcgbho610949f530518.pngbbox_to_anchor 的其他设置方法如图所示:

请先 登录 后评论