1

I would like to create a ridgeplot in seaborn with both columns of the dataframe to see any pattern as well as distribution among them. .

I tried the following but both the plots are coming as a single overlapped plot:

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

df = pd.DataFrame({'A': (10, 20, 20,30, 30, 30, 35, 35, 34, 33, 32, 31, 30), 'B': (15, 20, 20,30, 30, 30, 35, 35, 34, 33, 32, 31, 30)})
g = sns.FacetGrid(df)
g.map(sns.kdeplot, 'A', clip_on = False)
g.map(sns.kdeplot, 'B', clip_on = False)

Is there any other way to show them within a single plot but not overlapped, like the following???

g = sns.FacetGrid(df)
g.map(sns.kdeplot, ['A', 'B'], clip_on = False)

Thank you very much.

JohanC
  • 71,591
  • 8
  • 33
  • 66
JSVJ
  • 500
  • 3
  • 14
  • Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune Feb 16 '21 at 01:20
  • Please provide the expected [MRE - Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). We should be able to paste a single block of your code into file, run it, and reproduce your problem. This also lets us test any suggestions in your context. [Include your minimal data frame](https://stackoverflow.com/questions/52413246/how-to-provide-a-reproducible-copy-of-your-dataframe-with-to-clipboard) as part of the example. – Prune Feb 16 '21 at 01:35
  • @Prune Thanks, but the MRE already provided in the question :) . – JSVJ Feb 16 '21 at 01:39
  • There is no single block of self-contained code that will produce the output you have. – Prune Feb 16 '21 at 01:41
  • @Prune please check now. I believe the question is clear and self-contained code is provided. – JSVJ Feb 16 '21 at 09:20
  • What is exactly the question? Your title mentions "ridgeplot", but your question doesn't clarify what you mean by that. Are you referring to [seaborn's ridge plot example](https://seaborn.pydata.org/examples/kde_ridgeplot.html)? Did you try to implement it? – JohanC Feb 16 '21 at 14:07
  • Thanks; much better. Now let's see whether this can attract someone who's familiar with Seaborn. – Prune Feb 16 '21 at 19:10

1 Answers1

2

By converting the dataframe to long format, the column with the variable name can be used to create a separate subplot for each of the former columns.

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

df = pd.DataFrame({'A': (10, 20, 20, 30, 30, 30, 35, 35, 34, 33, 32, 31, 30),
                   'B': (15, 20, 20, 30, 30, 30, 35, 35, 34, 33, 32, 31, 30)})

df_long = df.melt(var_name='Column', value_name='Value')
g = sns.FacetGrid(df_long, row='Column', aspect=10, height=2)
g.map(sns.kdeplot, 'Value', clip_on=False, color='cornflowerblue', alpha=0.3, fill=True, lw=2)
g.map(plt.axhline, y=0, lw=2, color='cornflowerblue', clip_on=False)
plt.show()

resulting plot

df_long looks like

 
   Column  Value
0       A     10
1       A     20
2       A     20
3       A     30
...
22      B     33
23      B     32
24      B     31
25      B     30
JohanC
  • 71,591
  • 8
  • 33
  • 66