FiReTiTi
FiReTiTi

Reputation: 5898

Display multiple graphics with Seaborn

I have a csv file that I open with Pandas. It looks like this:

"Patient","Visit","Feature1","Feature2","Feature3"
"P1",0,?,?,?
"P1",1,?,?,?
"P2",0,?,?,?
"P2",1,?,?,?
"P2",2,?,?,?
"P3",0,?,?,?
"P3",1,?,?,?

? being a numerical value.

I know how to display one feature at a time (because they have various ranges) and separated per patient:

seaborn.stripplot(y="Feature1", x="Patient", data=features)

I would like to display ALL the plots inside a single image, like a mapping. How can I do it?

Upvotes: 0

Views: 117

Answers (1)

JohanC
JohanC

Reputation: 80509

Seaborn usually prefers the "long form" of the dataframe, which can be obtained via pandas' melt.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame({"Patient": ["P1", "P1", "P2", "P2", "P2", "P3", "P3"],
                   "Visit": [0, 1, 0, 1, 2, 0, 1],
                   "Feature1": np.random.normal(10, 2, 7),
                   "Feature2": np.random.normal(12, 2, 7),
                   "Feature3": np.random.normal(15, 2, 7)})
df_melted = df.melt(value_vars=["Feature1", "Feature2", "Feature3"], id_vars=["Patient", "Visit"],
                    var_name="Feature", value_name="Value")
sns.stripplot(data=df_melted, x="Patient", y="Value", hue="Feature", dodge=True)

plt.tight_layout()
plt.show()

seaborn stripplot with long form df

To get each feature in separate subplots, you can use catplot on the melted dataframe (use sharey=True if all features are in about the same range; use col_wrap=3 to start a new row every 3 columns):

sns.catplot(kind="strip", data=df_melted, x="Patient", y="Value", col="Feature", sharey=False)
plt.tight_layout()

catplot on melted dataframe

Upvotes: 2

Related Questions