two seaborn distplots one same axis

  • Last Update :
  • Techknowledgy :

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()

Suggestion : 2

histplot(), an axes-level function for plotting histograms, including with kernel density smoothing,This function combines the matplotlib hist function (with automatic calculation of a good default bin size) with the seaborn kdeplot() and rugplot() functions. It can also fit scipy.stats distributions and plot the estimated PDF over the data.,displot(), a figure-level function with a similar flexibility over the kind of plot to draw,Plot the distribution with a kernel density estimate and rug plot:

>>>
import seaborn as sns, numpy as np
   >>>
   sns.set_theme();
np.random.seed(0) >>>
   x = np.random.randn(100) >>>
   ax = sns.distplot(x)
>>>
import pandas as pd
   >>>
   x = pd.Series(x, name = "x variable") >>>
   ax = sns.distplot(x)
>>> ax = sns.distplot(x, rug = True, hist = False)
>>> from scipy.stats
import norm
   >>>
   ax = sns.distplot(x, fit = norm, kde = False)
>>> ax = sns.distplot(x, vertical = True)
>>> sns.set_color_codes() >>>
   ax = sns.distplot(x, color = "y")

Suggestion : 3

You an show a standard dataset from seaborn in histogram too.This is qutie a large dataset so only take one column.,Seaborn distplot lets you show a histogram with a line on it. This can be shown in all kinds of variations. We use seaborn in combination with matplotlib, the Python plotting module.,The plot below shows a simple distribution. It creats random values with random.randn().This will work if you manually define values too.,You can show all kinds of variations of the distplot. We use the subplot() method from the pylab module to show 4 variations at once.

1234567
import matplotlib.pyplot as pltimport seaborn as sns, numpy as npsns.set(rc = {
   "figure.figsize": (8, 4)
});
np.random.seed(0) x = np.random.randn(100) ax = sns.distplot(x) plt.show()
1234567891011121314151617181920
import matplotlib.pyplot as pltimport seaborn as sns, numpy as npfrom pylab
import * sns.set(rc = {
   "figure.figsize": (8, 4)
});
np.random.seed(0) x = np.random.randn(100) subplot(2, 2, 1) ax = sns.distplot(x) subplot(2, 2, 2) ax = sns.distplot(x, rug = False, hist = False) subplot(2, 2, 3) ax = sns.distplot(x, vertical = True) subplot(2, 2, 4) ax = sns.kdeplot(x, shade = True, color = "r") plt.show()
import matplotlib.pyplot as pltimport seaborn as snstitanic = sns.load_dataset('titanic') age1 = titanic['age'].dropna() sns.distplot(age1) plt.show()
import matplotlib.pyplot as pltimport seaborn as snstitanic = sns.load_dataset('titanic') age1 = titanic['age'].dropna() sns.distplot(age1, bins = 30, kde = False) plt.show()