Ksenia
Ksenia

Reputation: 59

Make colors in seaborn based on column names

I have a table with three columns: Names, A, B that I use to create a plot with the following code:

import seaborn as sns
sns.scatterplot(data=df, x="B", y="A")

enter image description here

How can make two different colors for dots based on column names? (i.e. A - red, B - green)

Upvotes: 0

Views: 1365

Answers (1)

sophocles
sophocles

Reputation: 13821

In a scatterplot, each point represents a pair values relating two sets of data, in your case A and B. Therefore, since each point on the graph is a pair, you can't colour different each individual point based on 'A' or 'B'.

What you can do, is set a different colour based on your Name column, using hue argument.

Below is an example using seaborn's tips dataset.

import seaborn as sns
sns.scatterplot(data=tips, x="total_bill", y="tip", hue="time")

enter image description here

In your case try something like:

sns.scatterplot(data=df, x="B", y="A",hue="Name")

https://seaborn.pydata.org/generated/seaborn.scatterplot.html

Upvotes: 1

Related Questions