artemis
artemis

Reputation: 7281

Seaborn catplot results in error by changing hue

I have a dataset that looks like this:

feature_1 feature_2 feature_3 feature_4 feature_5 feature_6 feature_7 feature_8
0 -0.0020185900105266514 -0.004525512052716703 0.004290147446159787 0.008121342033951665 0.019995812082180105 0.02034942055088337 -0.02236798581774497 -0.018665971326321824
1 0.008327938744324304 0.0057161731520134415 0.015149000101932132 0.014244686228342962 0.031266799783999905 0.02556201262830425 0.00491191281881069 0.002627771331087464
2 0.0056570911367399175 0.006780099460379361 -0.0038521559525533412 -0.0042372049750104175 0.025755417055772233 0.029050369619095566 -0.0016924684746490136 0.001915807620861465
3 -0.0066361424845156666 -0.006829267976941566 0.008195242107994306 0.00993842145208005 0.02794638215808405 0.025168342480038512 -0.013222987355723491 -0.011178407242310215
4 0.005111817323414786 0.002367954071875622 -0.0013140356150100757 -0.0027816139194379794 0.025028881734832177 0.029704777330334546 0.0073461329985677545 0.008414726948742138

I have been able to create a catplot that is almost perfect, like this:

sns.catplot(data=test_df, palette="dark", orient="h")

Resulting in:

enter image description here

However, I want the colors to change depending on the results of a list (which I could append to test_df). The list is as follows: classifications = ["class_1", "class_2", "class_1", "class_1", "class_2"]. Ideally, I'd like for the colors of the points to be different depending on the class.

Trying to add the hue parameter errors out, resulting in ValueError: Cannot use 'hue' without 'x' and 'y'

How can I change the colors of the points based on the values of the classifications list?

Upvotes: 1

Views: 289

Answers (1)

tdy
tdy

Reputation: 41457

You can add the class column and melt() into seaborn's preferred long form:

test_df["class"] = classifications
melted = test_df.melt("class", value_name="value", var_name="feature")

sns.catplot(data=melted, x="value", y="feature", hue="class", palette="dark", orient="h")

melted with hue

Upvotes: 1

Related Questions