参考官方文档,使用 sns.move_legend()
函数进行更改,demo如下:
ax = sns.histplot(一些绘制的参数)
sns.move_legend(ax, "best", title="新名字")
解释
-
使用
ax.legend(title='新名字')
或plt.legend(title='新名字')
是不行的。官方文档在此处中提到: this appears to move the legend, it actually replaces it with a new one。 -
move_legend()
函数的参数定义为seaborn.move_legend(obj, loc, **kwargs)
,里面这个**kwargs
就是plt.legend()
里面传入的**kwargs
。 -
关于
loc
的值的问题,参考此处
有如下值:best
upper right
upper left
lower left
lower rightt
right
center left
center right
lower center
upper center
center