0

I'm trying to use Seaborn to plot the contents for a Pandas DataFrame, but I cannot, for the life of me, figure out how to stack the distplots. The DataFrame I have looks something like this (simplified).

Image    | Obj1 | Obj2 | ... | ObjN
-----------------------------------
img1.jpg |   2  |   1  | ... | 0
-----------------------------------
img2.jpg |   5  |   5  | ... |  5
-----------------------------------
img3.jpg |   9  |   0  | ... |  1

Now, what I want to do is plot the distribution of the N-objects over the whole image set. With that I want to see how many images have Obj1 in them, how many have Obj2 int them, etc. It's a purely visual thing so don't think about how this might not be the best way to show said data.

In essence, what i want to do is something like:

for column in df.column:
   sns.distplot(column) # Stack these distributions together with different colors 

plt.show() # Display one plot with N-distribution plots inside

Wishing for an output similar to this (ish): Example plot


EDIT

Building on @con_u's answer I've generated the following plots:

No Zoom Zoomed In on the Origin

They are rather useless though with the vertical bars that can be seen in image 1. I know the distribution is heavily skewed towards the lower numbers (counts), so maybe I'm out of luck and need to reconsider my plotting options.

andkir
  • 35
  • 1
  • 1
  • 6
  • Seems like this question has been answered before: https://stackoverflow.com/questions/32899463/how-can-i-overlay-two-graphs-in-seaborn. Will that answer your question? – Hielke Walinga Oct 25 '18 at 08:00
  • Thanks @HielkeWalinga, but sadly this does not work in my case. – andkir Oct 25 '18 at 08:35
  • Maybe try some log scaling on one of axis? Can you provide code so we can reproduce your results? – Hielke Walinga Oct 25 '18 at 08:41
  • Sadly I cannot, due to limitations. Log scaling would definitely be a good idea and would fix the horizontal scaling, yet it doesn't solve the problem with the bars still showing up. I'll tweak it a bit and see how I do. Thanks – andkir Oct 25 '18 at 08:52
  • Those bars seem to be coming from the `rug=True`. You can change that to `rug=False`. – Hielke Walinga Oct 25 '18 at 08:57

1 Answers1

1

This works for me.

# ----------------------------------------------------------------------
# Import library
# ----------------------------------------------------------------------
import numpy as np
import pandas as pd
import seaborn as sns
import random

# ----------------------------------------------------------------------
# Create an artificial dataframe
# ----------------------------------------------------------------------
df = pd.DataFrame({'image':np.arange(100) + 1,
                  'obj1':np.random.randint(low=1, high=100, size=100),
                  'obj2':np.random.randint(low=1, high=100, size=100),
                  'obj3':np.random.randint(low=1, high=100, size=100),
                  'obj4':np.random.randint(low=1, high=100, size=100)})

df = df.set_index('image')
df.head(10)

# ----------------------------------------------------------------------
# Plot the distributions (column-wise) (if you just want some columns)
# ----------------------------------------------------------------------
sns.distplot(df[['obj1']], hist=False, rug=True)
sns.distplot(df[['obj2']], hist=False, rug=True)
sns.distplot(df[['obj3']], hist=False, rug=True)
sns.distplot(df[['obj4']], hist=False, rug=True)
sns.plt.show()

# ----------------------------------------------------------------------
# Plot the distributions (column-wise) (looping method)
# ----------------------------------------------------------------------
for col in df.columns:
  sns.distplot(df[[col]], hist=False, rug=True)

You may also want to check the relevant ones here:

enter image description here

TechWizard
  • 346
  • 1
  • 7
  • Thanks. This is similar to how I tried it initially (except I forgot the double brackets for reffing the columns). However I end up with pretty ugly plots that way. I've added images of the plots in the initial question (due to comment limitations). – andkir Oct 25 '18 at 08:35