Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two seaborn distplots one same axis

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

like image 995
Astrid Avatar asked Jan 29 '16 16:01

Astrid


People also ask

What is the difference between DistPlot and Displot?

displot() is the new distplot() with better capabilities and distplot() is deprecated starting from this Seaborn version. With the new displot() function in Seaborn, the plotting function hierarchy kind of of looks like this now covering most of the plotting capabilities.

What is Displot in Seaborn?

We use a displot (also known as a distribution plot) to represent data in histogram form. It is a univariant set of collected data, which means the data distribution of one variable will be shown against another variable. In Python, we use the Seaborn library with Matplotlib for data visualization.

What is density in DistPlot?

Kernel Density Estimation (KDE) is a way to estimate the probability density function of a continuous random variable. It is used for non-parametric analysis. Setting the hist flag to False in distplot will yield the kernel density estimation plot.


1 Answers

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.

like image 111
Primer Avatar answered Oct 19 '22 11:10

Primer