在sns散点图中更改图例

问题描述

我正在使用seaborn的散点图创建以下可视化效果

enter image description here

sns.scatterplot(x="page",y="length",hue="% of marginal annotations",size="% of marginal annotations",legend="brief",data=df,palette = cmap,s=40,alpha=.6,sizes=(20,200)).set(xlabel='Folio number',ylabel='Length')

设置为“简短”时,图例会将数据分解为从0到... 120的40%增量!我担心这个传说会误导读者,使我认为我的某些数据点超过100%(从来没有这样)。如何更改图例,使其显示从0到100%的不同大小?

解决方法

如果您确信自己的"% of marginal annotations"数据点中没有一个超过100%,则只需添加.legend(range(0,125,25)),本质上就是定义图例中应包含哪些值。

fig,ax = plt.subplots()
ax = sns.scatterplot(x="page",y="length",hue="% of marginal annotations",size="% of marginal annotations",legend="brief",data=df,palette = cmap,s=40,alpha=.6,sizes=(20,200))
ax.set(xlabel='Folio number',ylabel='Length')
ax.legend(range(0,25))