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

Time:05-28

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

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])

CodePudding user response:

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

  • Related