How to draw python seaborn histograms in matplotlib subplots?

Asked
Active3 hr before
Viewed126 times

7 Answers

seabornmatplotlibhistogramspythonsubplots
90%

Sometimes it is helpful to compare different views of data side by side, To this end, Matplotlib has the concept of subplots: groups of smaller axes that can exist together within a single figure

Example_snippet/controller/utility/_seaborn.js/ %matplotlib inline import matp. . .
% matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
import numpy as np
load more v
88%

Since we don't have a Figure area to plot inside, Python first creates a Figure object, And since the Figure doesn't start with any Axes to draw the histogram onto, an Axes object is created inside the Figure

Example_snippet/controller/utility/_seaborn.js/ plt.hist(data = df, x = 'num_v. . .
plt.hist(data = df, x = 'num_var')
load more v
72%

Example_snippet/controller/utility/_seaborn.js/ sns.distplot(gapminder['lifeEx. . .
sns.distplot(gapminder['lifeExp'], kde = False, color = 'red', bins = 100)
plt.title('Life Expectancy', fontsize = 18)
plt.xlabel('Life Exp (years)', fontsize = 16)
plt.ylabel('Frequency', fontsize = 16)
load more v
65%

Example_snippet/controller/utility/_seaborn.js/ # libraries & dataset import s. . .
# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background(use sns.set_theme() if seaborn version 0.11 .0 or above)
sns.set(style = "darkgrid")
df = sns.load_dataset("iris")

sns.histplot(data = df, x = "sepal_length", color = "skyblue", label = "Sepal Length", kde = True)
sns.histplot(data = df, x = "sepal_width", color = "red", label = "Sepal Width", kde = True)

plt.legend()
plt.show()
load more v
75%

Histogram grouped by categories in separate subplots,Seaborn Histogram and Density Curve on the same plot,Histogram grouped by categories in same plot,You can plot multiple histograms in the same plot, This can be useful if you want to compare the distribution of a continuous variable grouped by different categories

Example_snippet/controller/utility/_seaborn.js/ import numpy as np x = np.rand. . .
import numpy as np
x = np.random.randint(low = 0, high = 100, size = 100)

# Compute frequency and bins
frequency, bins = np.histogram(x, bins = 10, range = [0, 100])

# Pretty Print
for b, f in zip(bins[1: ], frequency):
   print(round(b, 1), ' '.join(np.repeat('*', f)))
Step 2 continued with 10.0 * * * * * * * * * 20.0 * . . .
10.0 * * * * * * * * *
   20.0 * * * * * * * * * * * * *
   30.0 * * * * * * * * *
   40.0 * * * * * * * * * * * * * * *
   50.0 * * * * * * * * *
   60.0 * * * * * * * * *
   70.0 * * * * * * * * * * * * * * * *
   80.0 * * * * *
   90.0 * * * * * * * * *
   100.0 * * * * * *
Step 3 continued with import matplotlib.pyplot as pl. . .
import matplotlib.pyplot as plt %
   matplotlib inline
plt.rcParams.update({
   'figure.figsize': (7, 5),
   'figure.dpi': 100
})

# Plot Histogram on x
x = np.random.normal(size = 1000)
plt.hist(x, bins = 50)
plt.gca().set(title = 'Frequency Histogram', ylabel = 'Frequency');
Step 4 continued with import pandas as pd df = pd.re. . .
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/diamonds.csv')
df.head()
Step 5 continued with x1 = df.loc[df.cut=='Ideal', '. . .
x1 = df.loc[df.cut == 'Ideal', 'depth']
x2 = df.loc[df.cut == 'Fair', 'depth']
x3 = df.loc[df.cut == 'Good', 'depth']

kwargs = dict(alpha = 0.5, bins = 100)

plt.hist(x1, ** kwargs, color = 'g', label = 'Ideal')
plt.hist(x2, ** kwargs, color = 'b', label = 'Fair')
plt.hist(x3, ** kwargs, color = 'r', label = 'Good')
plt.gca().set(title = 'Frequency Histogram of Diamond Depths', ylabel = 'Frequency')
plt.xlim(50, 75)
plt.legend();
Step 6 continued with # Normalize kwargs = dict(alph. . .
# Normalize
kwargs = dict(alpha = 0.5, bins = 100, density = True, stacked = True)

# Plot
plt.hist(x1, ** kwargs, color = 'g', label = 'Ideal')
plt.hist(x2, ** kwargs, color = 'b', label = 'Fair')
plt.hist(x3, ** kwargs, color = 'r', label = 'Good')
plt.gca().set(title = 'Probability Histogram of Diamond Depths', ylabel = 'Probability')
plt.xlim(50, 75)
plt.legend();
Step 7 continued with # Import Data df = pd.read_csv. . .
# Import Data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/diamonds.csv')

# Plot
fig, axes = plt.subplots(1, 5, figsize = (10, 2.5), dpi = 100, sharex = True, sharey = True)
colors = ['tab:red', 'tab:blue', 'tab:green', 'tab:pink', 'tab:olive']

for i, (ax, cut) in enumerate(zip(axes.flatten(), df.cut.unique())):
   x = df.loc[df.cut == cut, 'depth']
ax.hist(x, alpha = 0.5, bins = 100, density = True, stacked = True, label = str(cut), color = colors[i])
ax.set_title(cut)

plt.suptitle('Probability Histogram of Diamond Depths', y = 1.05, size = 16)
ax.set_xlim(50, 70);
ax.set_ylim(0, 1);
plt.tight_layout();
Step 8 continued with import seaborn as sns sns.set_. . .
import seaborn as sns
sns.set_style("white")

# Import data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/diamonds.csv')
x1 = df.loc[df.cut == 'Ideal', 'depth']
x2 = df.loc[df.cut == 'Fair', 'depth']
x3 = df.loc[df.cut == 'Good', 'depth']

# Plot
kwargs = dict(hist_kws = {
   'alpha': .6
}, kde_kws = {
   'linewidth': 2
})

plt.figure(figsize = (10, 7), dpi = 80)
sns.distplot(x1, color = "dodgerblue", label = "Compact", ** kwargs)
sns.distplot(x2, color = "orange", label = "SUV", ** kwargs)
sns.distplot(x3, color = "deeppink", label = "minivan", ** kwargs)
plt.xlim(50, 75)
plt.legend();
Step 9 continued with # Import data df = pd.read_csv. . .
# Import data
df = pd.read_csv('https://raw.githubusercontent.com/selva86/datasets/master/diamonds.csv')
x1 = df.loc[df.cut == 'Ideal', ['depth']]
x2 = df.loc[df.cut == 'Fair', ['depth']]
x3 = df.loc[df.cut == 'Good', ['depth']]

# plot
fig, axes = plt.subplots(1, 3, figsize = (10, 3), sharey = True, dpi = 100)
sns.distplot(x1, color = "dodgerblue", ax = axes[0], axlabel = 'Ideal')
sns.distplot(x2, color = "deeppink", ax = axes[1], axlabel = 'Fair')
sns.distplot(x3, color = "gold", ax = axes[2], axlabel = 'Good')
plt.xlim(50, 75);
Step 10 continued with # Groupby: cutwise median pric. . .
# Groupby: cutwise median
price = df[['cut', 'price']].groupby('cut').median().round(2)
price
Step 11 continued with fig, axes = plt.subplots(figsi. . .
fig, axes = plt.subplots(figsize = (7, 5), dpi = 100)
plt.bar(price.index, height = price.price)
plt.title('Barplot of Median Diamond Price');
Step 12 continued with import seaborn as sns df = sns. . .
import seaborn as sns
df = sns.load_dataset('iris')
Step 13 continued with # Solution import seaborn as s. . .
# Solution
import seaborn as sns
df = sns.load_dataset('iris')

plt.subplots(figsize = (7, 6), dpi = 100)
sns.distplot(df.loc[df.species == 'setosa', "sepal_length"], color = "dodgerblue", label = "Setosa")
sns.distplot(df.loc[df.species == 'virginica', "sepal_length"], color = "orange", label = "virginica")
sns.distplot(df.loc[df.species == 'versicolor', "sepal_length"], color = "deeppink", label = "versicolor")

plt.title('Iris Histogram')
plt.legend();
load more v
40%

Python source code: [download source: distplot_options,py],Distribution plot options, Page Distribution plot options

Example_snippet/controller/utility/_matplotlib.js/ import numpy as np import seab. . .
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style = "white", palette = "muted", color_codes = True)
rs = np.random.RandomState(10)

# Set up the matplotlib figure
f, axes = plt.subplots(2, 2, figsize = (7, 7), sharex = True)
sns.despine(left = True)

# Generate a random univariate dataset
d = rs.normal(size = 100)

# Plot a simple histogram with binsize determined automatically
sns.distplot(d, kde = False, color = "b", ax = axes[0, 0])

# Plot a kernel density estimate and rug plot
sns.distplot(d, hist = False, rug = True, color = "r", ax = axes[0, 1])

# Plot a filled kernel density estimate
sns.distplot(d, hist = False, color = "g", kde_kws = {
   "shade": True
}, ax = axes[1, 0])

# Plot a histogram and kernel density estimate
sns.distplot(d, color = "m", ax = axes[1, 1])

plt.setp(axes, yticks = [])
plt.tight_layout()
22%

I've produced two code snippets for seaborn histograms in subplots, First one,Pleas advise me what I'm doing wrong in the first snippet

Example_snippet/controller/utility/_matplotlib.js/ fig, axes = plt.subplots(1, 2,. . .
fig, axes = plt.subplots(1, 2, figsize = (15, 5), sharey = False)
sns.histplot(ax = axes[1, 0], x = data.age)
sns.histplot(ax = axes[0, 0], x = data.duration)
plt.show()
load more v