Home > Net >  scatterplot: set hue to value of dataframe cells
scatterplot: set hue to value of dataframe cells

Time:12-14

I made up this example data:

import numpy as np
import pandas as pd
import seaborn as sns
y = np.random.rand(10,3)
df = pd.DataFrame(y, columns=["1", "2", "3"])

df.head()

Now i want to scatterplot this example data. I have tried around with seaborn: Easiest way is this

sns.scatterplot(data=df)

which gives me nearly what i want:

scatterplot1

Now my question: Is it possible to change y with the hue? So that i have the my Colums as y-axis and the hue is based on the values of the cells.

scatterplot2

CodePudding user response:

You can use the melt function with argument ignore_index=False:

y = np.random.rand(10,3)
df = pd.DataFrame(y, columns=["1", "2", "3"])
df = pd.melt(df, var_name="y_val", value_name='hue', ignore_index=False)
sns.scatterplot(x=df.index, y="y_val", hue="hue", data=df)
plt.show()

Output: enter image description here

  • Related