wam_salley
wam_salley

Reputation: 33

Seaborn Scatterplot Color by X and Y

I am having trouble coloring what should be a simple scatter plot. I am plotting two comparing columns in my pandas data frame, but I would like to color the scatter plot by X scatter and by Y scatter. So X scatter would be red and Y scatter will be black.

Here is a snippet of what I have done so far. This is with sns.lmplot, but I have tried with sns.scatterplot also.

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)

also here is a image of how my dataframe is set up:

enter image description here

Upvotes: 0

Views: 6130

Answers (1)

busybear
busybear

Reputation: 10590

I think you are confusing the parameters of lmplot. Also, you could use regplot instead, as your not using the features that make lmplot different from regplot. Regardless, it seems you should be using your 'time' column as your x-values and 'xco2' and 'xco2_part' as y-values. In this case, you can make two plotting calls and set your color parameter. So something like this:

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

Here's an example:

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

Upvotes: 3

Related Questions