如何解决海生散点图
我想为seaborn的散点图做两件事。
- 更改图例标题,并从图例中删除系列名称。因此,请删除图例中显示的
petal_length
- 更改图例的数字输出。更改为[0,4,8]
我该怎么做?
import seaborn as sns
cmap = sns.light_palette("green",as_cmap=True)
iris = sns.load_dataset("iris")
p=sns.scatterplot("sepal_length","sepal_width",data=iris,hue="petal_length",palette = cmap
)
p.set(xlabel='Sepal Len',ylabel='Sepal Width')
p.legend(title="Petal Length")
plt.show()
解决方法
为地块添加标题 How to add title to seaborn boxplot
删除图例标题: Remove seaborn lineplot legend title
更改图例如何“存储桶”: 您已将色调设置为数据的一列。我会将长度分成新列中所需的组,然后将色相设置为新列。
,使用get_legend_handles_labels()
,您可以获取要在图例中使用的手柄和标签的列表。然后,您可以创建图例,同时跳过句柄和标签的所有其他元素。
from matplotlib import pyplot as plt
import seaborn as sns
cmap = sns.light_palette("green",as_cmap=True)
iris = sns.load_dataset("iris")
ax = sns.scatterplot("sepal_length","sepal_width",data=iris,hue="petal_length",palette=cmap)
ax.set_xlabel('Sepal Len'),ax.set_ylabel('Sepal Width')
handles,labels = ax.get_legend_handles_labels()
ax.legend(handles[1::2],labels[1::2],title="Petal Length")
plt.show()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。