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

JOR*_*974 6 python matplotlib seaborn

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")
Run Code Online (Sandbox Code Playgroud)

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

我现在的形象

sac*_*cuL 8

x要在自定义和y坐标处添加点,请添加matplotlib.pyplot.scatter您的坐标:

plt.scatter(x=3, y=0.5, color='r')
Run Code Online (Sandbox Code Playgroud)

为最后一个点着色,请.iloc在数据上使用定位器:

plt.scatter(iris.petal_length.iloc[-1], iris.petal_width.iloc[-1], color='r')
Run Code Online (Sandbox Code Playgroud)

请注意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')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述