Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How To Combine Two Seaborn catplot s with shared x-axis

i have a dataset with

Categories Model_1 Model_2
A 0.49 0.13
B 0.52 0.13
C 0.46 0.03

I want to plot one figure with same x axis=Categories and y1=Model_1 and y2=Model_2 and I want result something like this :

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I use this lines of code but he gives me 2 plots in differents figures

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

df = pd.read_csv('Dataset.csv')

sns.catplot(x="Categories", y="Model_1",data=df,kind="point",markers=["o"],linestyles=["-"])
sns.catplot(x="Categories", y="Models_2",data=df,kind="point",markers=["^"],linestyles=["--"],ax = axs[1])

>Solution :

Often it helps to reshape your data into a long format for plotting. You can do this with pd.melt. You wouldn’t get lightly shaded confidence intervals like your sample image unless you had multiple values per category/model.

import seaborn as sns
import pandas as pd
df = pd.DataFrame({
    'Categories': ['A', 'B', 'C'],
    'Model_1': [0.49, 0.52, 0.46],
    'Model_2': [0.13, 0.13, 0.03]})

df = df.melt(id_vars='Categories', var_name='Model')
print(df)
sns.catplot(data=df,
            x='Categories',
            y='value',
            hue='Model',
            kind='point',
            markers=['o','^'],
            linestyles=['-','--'])

Output

  Categories    Model  value
0          A  Model_1   0.49
1          B  Model_1   0.52
2          C  Model_1   0.46
3          A  Model_2   0.13
4          B  Model_2   0.13
5          C  Model_2   0.03

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading