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

标签 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")

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

My current Image

最佳答案

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

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

为最后一点着色,请使用 .iloc您的数据定位器:

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

注意iloc 定位器来自 pandasplt.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')

enter image description here

关于python - 使用seaborn,如何将不同颜色的数据点添加到散点图或更改为最后一个数据点的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50438077/

相关文章:

python - 获取默认线条颜色循环

python - 未应用 Matplotlib 样式

python - 使用 Python 正则表达式提取带连字符的电话号码

python - 如何使用 pandas 替换所有列中的所有字符串?

python - pyplot 轮廓 : How can I make the colors in the chart continuous?

python - 如何径向 'sweep out' 1D 数组在 python 中绘制 3d 图形? (表示波函数)

python - Seaborn 将 kwargs 传递给 plt.boxplot()

python - Seaborn 显示在 Pandas 列中找不到的值

python - 查找 pandas 中存在多个月的列值

python - 需要在 pythonanywhere 上杀死卡住的进程