繁体   English   中英

使用seaborn,如何将不同颜色的数据点添加到散点图或更改为最后一个数据点的颜色?

[英]Using seaborn, how can I add a data point of a different color to my scatterplot or change to color of last data point?

import seaborn as sns
iris = sns.load_dataset("iris")    
grid = sns.JointGrid(iris.petal_length, iris.petal_width, space=0, size=6, ratio=50)
    grid.plot_joint(plt.scatter, color="g")

上面的代码将基于Iris数据集创建散点图。 我想在[3,.05]处添加另一个红色的数据点; 或将数据集中的最后一点设为红色。 我该怎么做呢?

我当前的图片

要在自定义xy坐标处添加点 ,请添加matplotlib.pyplot.scatter和坐标:

plt.scatter(x=3, y=0.5, color='r')

为了给最后一点 .iloc ,请在数据上使用.iloc定位符:

plt.scatter(iris.petal_length.iloc[-1], iris.petal_width.iloc[-1], color='r')

请注意iloc定位器来自pandas ,而plt.scatter来自matplotlib.pyplot 这两个都是seaborn的强制性依赖项,因此,如果您使用的是seaborn,则一定要将它们安装在计算机上。

例如:

import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")    
grid = sns.JointGrid(iris.petal_length, iris.petal_width, space=0, size=6, ratio=50)
grid.plot_joint(plt.scatter, color="g")
# add your point
plt.scatter(x=3, y=0.5, color='r')
# or
# plt.scatter(iris.petal_length.iloc[-1], iris.petal_width.iloc[-1], color='r')

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM