Python 在Seaborn中,一种特定的颜色能否覆盖已经基于另一列的色调?

Python 在Seaborn中,一种特定的颜色能否覆盖已经基于另一列的色调?,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我试图在seaborn中绘图,以便色调根据一列定义我的数据点,如果另一列的值为1,则应覆盖色调 我用K表示异常检测,所以我为不同的簇绘制色调,这很好,但现在在同一个图中,如果行的“异常”列=1,那么数据点,我想用红色显示。这可能吗 df = pd.DataFrame({'var1': [1, 2, 3, 4, 5, 6, 7], 'var2': [100, 200, 300, 400, 500, 600, 700],

我试图在seaborn中绘图,以便色调根据一列定义我的数据点,如果另一列的值为1,则应覆盖色调

我用K表示异常检测,所以我为不同的簇绘制色调,这很好,但现在在同一个图中,如果行的“异常”列=1,那么数据点,我想用红色显示。这可能吗

df = pd.DataFrame({'var1': [1, 2, 3, 4, 5, 6, 7], 
                    'var2': [100, 200, 300, 400, 500, 600, 700], 
                    'cluster': [0,0,0,0,0,1,1], 'anomalies':[1,1,1,0,0,0,0]})
sns.scatterplot(x='var1', y='var2', hue='cluster', data=df)
在上面的代码中,我应该能够根据label1值传递自定义颜色 期望:我应该能够在一列的基础上绘制色调,在另一列的基础上绘制自定义颜色

编辑:由于@ImportanceOfBeingErnest询问使用seaborn而不是matplotlib的原因,我想使用seaborn而不是matplotlib,因为绘图更干净。如。


如果要使用matplotlib,它可以如下所示。为异常创建一个散点,为其他散点创建一个散点

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'var1': [1, 2, 3, 4, 5, 6, 7], 
                    'var2': [100, 200, 300, 400, 500, 600, 700], 
                    'cluster': [0,0,0,0,0,1,1], 'anomalies':[1,1,1,0,0,0,0]})

plt.style.use("seaborn-whitegrid")
cmap = sns.cubehelix_palette(256, as_cmap=True)

sc1 = plt.scatter(x='var1', y='var2', c='cluster', data=df[df['anomalies'] == 0], cmap=cmap)
sc2 = plt.scatter(x='var1', y='var2', color="red", data=df[df['anomalies'] == 1])  

h, l = sc1.legend_elements()
plt.legend(h+[sc2], l+["anomalies"])
plt.show()

正如@ImportanceOfBeingErnest所建议的,这可以通过使用两个MatPlotLib来实现,但问题是,我不能使用seaborn提供的色调属性。 因此,为了得到seaborn的色调特性,我想用seaborn本身来解决它。最后,通过一些尝试和错误,我通过以下方式结合seaborn和matplotlib解决了这个问题:

import pandas as pd
import matplotlib.pyplot as plt

fig,ax = plt.subplots(1, 1, figsize=(19,4))

df = pd.DataFrame({'var1': [1, 2, 3, 4, 5, 6, 7], 
                    'var2': [100, 200, 300, 400, 500, 600, 700], 
                    'cluster': [0,0,0,0,0,1,1], 'anomalies':[1,0,1,0,0,0,0]})

sns.scatterplot(x='var1', y='var2', hue='cluster', data=df)
plt.scatter(x='var1', y='var2', color="red", data=df[df['anomalies'] == 1])

# plot the legend
legend = plt.legend()
legend.get_texts()[0].set_text("")
legend.get_texts()[3].set_text('anomaly')

因此,在散点图中应用色调后,我可以使用matplotlib仅覆盖图中的特定数据,并将这些特定颜色应用于这些数据点

问题实际上与机器学习无关或
k-means
-请不要垃圾发送不相关的标签(已删除)。我猜你想引入一个额外的色调,在本例中,
[2,2,2,0,0,1,1]
@ImportanceOfBeingErnest但是我在哪里添加这个额外的色调呢?在一个名为“mycolumn”的列中,并将其用作
hue=“mycolumn”
@ImportanceOfBeingErnest抱歉,我没有完全理解它。。假设在我上面给出的示例中,sns.scatterplot(x='var1',y='var2',hue='cluster',data=df)现在cluster列提供色调。但是我如何确保异常为class='1'的列覆盖色调。所以我可以用红色显示异常,剩下的数据由色调+1定义,用于两个matplotlibs散点图。但我想拥有seaborn的色调属性。在我的回答中能够使用seaborn和matplotlib解决问题。seaborn中的
色调和matplotlib中的类绘制散点没有区别。是的,你是对的,色调与matplotlib中的c相同,但matplotlib的绘图没有seaborn干净,因此,想要使用seaborn实现这一目的matplotlib的绘图有什么不干净的地方?因为我无法在评论部分添加图像,所以我用图像更新了问题,这表明seaborn绘图比matplotlib干净得多。这就是我想使用seaborn的原因