8

I am trying to figure a nice way to plot two distplots (from seaborn) on the same axis. It is not coming out as pretty as I want since the histogram bars are covering each other. And I don't want to use countplot or barplot simply because they don't look as pretty. Naturally if there is no other way I shall do it in that fashion, but distplot looks very good. But, as said, the bars are now covering each other (see pic).

Thus is there any way to fit two distplot frequency bars onto one bin so that they do not overlap? Or placing the counts on top of each other? Basically I want to do this in seaborn:

enter image description here

Any ideas to clean it up are most welcome. Thanks.

MWE:

sns.set_context("paper",font_scale=2)
sns.set_style("white")
rc('text', usetex=False)
fig, ax = plt.subplots(figsize=(7,7),sharey=True)
sns.despine(left=True)

mats=dict()
mats[0]=[1,1,1,1,1,2,3,3,2,3,3,3,3,3]
mats[1]=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5]
N=max(max(set(mats[0])),max(set(mats[1])))

binsize = np.arange(0,N+1,1)
B=['Thing1','Thing2']
for i in range(len(B)):
    ax = sns.distplot(mats[i],
                      kde=False,
                      label=B[i],
                      bins=binsize)

ax.set_xlabel('My label')
ax.get_yaxis().set_visible(False)
ax.legend()
plt.show()

enter image description here

Astrid
  • 1,846
  • 4
  • 26
  • 48
  • Are you asking how to draw a [stacked histogram](http://matplotlib.org/examples/statistics/histogram_demo_multihist.html)? – mwaskom Jan 29 '16 at 20:55
  • No I am asking how to either do bottom right or top left in your link. I am asking how to do this in seaborn. – Astrid Jan 29 '16 at 21:35
  • 3
    Seaborn uses matplotlib. There's no "in seaborn". – mwaskom Jan 29 '16 at 21:42
  • 1
    Very true, but seaborn does have its own functions, like the one above. I would rather make that work before falling back onto matplotlib. – Astrid Jan 29 '16 at 22:08
  • 1
    See also [this](https://stackoverflow.com/questions/46045750/python-distplot-with-multiple-distributions) – borgr Jan 03 '19 at 13:26

1 Answers1

7

As @mwaskom has said seaborn is wrapping matplotlib plotting functions (well to most part) to deliver more complex and nicer looking charts.

What you are looking for is "simple enough" to get it done with matplotlib:

sns.set_context("paper", font_scale=2)
sns.set_style("white")
plt.rc('text', usetex=False)
fig, ax = plt.subplots(figsize=(4,4))
sns.despine(left=True)

# mats=dict()
mats0=[1,1,1,1,1,2,3,3,2,3,3,3,3,3]
mats1=[3,3,3,3,3,4,4,4,5,6,1,1,2,3,4,5,5,5]
N=max(mats0 + mats1)

# binsize = np.arange(0,N+1,1)
binsize = N
B=['Thing1','Thing2']

ax.hist([mats0, mats1], binsize, histtype='bar', 
        align='mid', label=B, alpha=0.4)#, rwidth=0.6)

ax.set_xlabel('My label')
ax.get_yaxis().set_visible(False)
# ax.set_xlim(0,N+1)
ax.legend()
plt.show()

Which yields:

enter image description here

You can uncomment ax.set_xlim(0,N+1) to give more space around this histogram.

Primer
  • 10,092
  • 5
  • 43
  • 55