3

How can I make make two separate Seaborn kdeplots for all the columns of a pandas dataframe:

  • A pandas dataframe (df) with six columns (e.g. below):

df.columns = ["A", "B", "C", "D", "E", "F"]

I tried the following code, but that did not work. Any tips for the code below?

df = sns.load_dataset("df")
g = sns.FacetGrid(df)
g.map(s.skdeplot, "df.columns");
Tjeerd Tim
  • 71
  • 1
  • 8
  • 1
    add code to your example that generates your dataframe from scratch (e.g., without reading any files) – Paul H Oct 14 '15 at 05:00
  • 1
    KDEs are (nearly) continuous calculations, what do you mean by "points below 0.5". Do you want the portions of the KDE below and above the threshold to be different colors? – Paul H Oct 14 '15 at 05:04
  • @PaulH. Changed the question a bit. Please see above. Hope you can help out. – Tjeerd Tim Oct 14 '15 at 09:52
  • 1
    `df = sns.load_dataset("df")` is throwing an error. Can you make your example reproducible from scratch? – Paul H Oct 14 '15 at 16:56
  • 1
    http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Paul H Oct 14 '15 at 16:58
  • use the same approach as this answer: http://stackoverflow.com/questions/33049884/how-to-plot-2-seaborn-lmplots-side-by-side – Paul H Oct 17 '15 at 00:55

2 Answers2

4

I think you need to use pd.melt:

df = pd.DataFrame({'id1' :np.random.randint(3,size=1000),
               'id2' :['ABC'[i] for i in np.random.randint(3,size=1000)],
               'val1':np.random.normal(loc=1, size=1000),
               'val2':np.random.normal(loc=2, size=1000),
               'val3':np.random.normal(loc=3, size=1000)})

g = sns.FacetGrid(pd.melt(df,
                      id_vars=['id1','id2'],
                      value_vars=['val1','val2','val3']),
              hue='id1',col='id2',row='variable')
g.map(sns.kdeplot,'value')

enter image description here

thor
  • 21,418
  • 31
  • 87
  • 173
jharting
  • 71
  • 3
2

Your problem is here:

ax(i) = sns.kdeplot(dftouse[column], c = colorUp(dftouse[column]))

ax(i) is a function call. You are trying to assign something to it. That is not correct.

I'm not familiar with matplotlib, just Python. Perhaps did you mean ax[i]? If ax is an array or a dict, then this might be correct.

RobertB
  • 1,879
  • 10
  • 17
  • Changed the question a bit. Please see above. Hope you can help out. – Tjeerd Tim Oct 14 '15 at 09:53
  • 3
    Seems strange that you have `df.columns` in quotes, but again, I'm not familiar with that package.. (Also, you changed the question entirely. If my comment was helpful, it might have been polite to give me an up-vote or credit for answering it, and then asking a different question in a different thread. I am trying to build some reputation after all.) – RobertB Oct 14 '15 at 16:39