X和Y的Seaborn散点图颜色

时间:2020-06-19 20:07:46

标签: python pandas matplotlib seaborn scatter-plot

我在为简单的散点图着色时遇到了麻烦。我正在熊猫数据框中绘制两个比较列,但是我想通过X散点图和Y散点图来绘制散点图。因此,X散射为红色,Y散射为黑色。

这里是我到目前为止所做的摘要。这是与sns.lmplot一起使用的,但是我也尝试了sns.scatterplot。

fig, ax = plt.subplots(figsize=(10, 5))

x=df_layer10s2['xco2'].values
y=df_layer10s2['xco2_part'].values
col = (if x then 'r', else 'black')
ax= sns.lmplot(x='xco2',y='xco2_part',data=df_layer10s2)
# plt.ylim(389,404)
# plt.xlim(389,404)

这也是我的数据框设置方式的图像:

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您在混淆lmplot的参数。另外,您可以改用regplot,因为您不使用使lmplotregplot不同的功能。无论如何,似乎您应该将'time'列用作x值,并将'xco2''xco2_part'用作y值。在这种情况下,您可以进行两次绘图调用并设置color参数。像这样:

sns.regplot(x='time', y='xco2', data=df_layer10s2, color='r')
sns.regplot(x='time', y='xco2_part', data=df_layer10s2, color='k')

这是一个例子:

np.random.seed(42)
time = np.random.random(50)
y0 = np.random.random(50)
y1 = np.random.random(50)
df = pd.DataFrame({'time': time, 'y0': y0, 'y1': y1})

sns.regplot(x='time', y='y0', data=df, color='r')
sns.regplot(x='time', y='y1', data=df, color='k')

enter image description here