Plotting different predictions with same column names and categories Python/Seaborn

Viewed 80

I have df with different groups. I have two predictions (iqr, median).

cntx_iqr    pred_iqr    cntx_median pred_median
18-54        83          K18-54         72
R18-54       34          R18-54         48
25-54        33          18-34          47
K18-54       29          18-54          47
18-34        27          R25-54         29
K18-34       25          25-54          23
K25-54       24          K25-54         14
R18-34       22          R18-34          8
R25-54       17          K18-34          6         

Now I want to plot them using seaborn and I have melted data for pilots. However, it does not look right to me.

pd.melt(df, id_vars=['cntx_iqr', 'cntx_median'], value_name='category', var_name="kind")

I am aiming to compare predictions (pred_iqr,pred_median) from those 2 groups (cntx_iqr, cntx_median) maybe stack barplot or some other useful plot to see how each group differs for those 2 predictions.

any help/suggestion would be appreciated

Thanks in advance

1 Answers

Not sure how you obtained the data frame, but you need to match the values first:

df = df[['cntx_iqr','pred_iqr']].merge(df[['cntx_median','pred_median']],
                                  left_on="cntx_iqr",right_on="cntx_median")

df.head()

    cntx_iqr    pred_iqr    cntx_median pred_median
0   18-54   83  18-54   47
1   R18-54  34  R18-54  48
2   25-54   33  25-54   23
3   K18-54  29  K18-54  72
4   18-34   27  18-34   47

Once you have this, you can just make a scatterplot:

sns.scatterplot(x = 'pred_iqr',y = 'pred_median',data=df)

enter image description here

The barplot requires a bit of pivoting, but should be:

sns.barplot(x = 'cntx_iqr', y = 'value', hue='variable',
            data = df.melt(id_vars='cntx_iqr',value_vars=['pred_iqr','pred_median']))

enter image description here

Related