what are differences between histtype='bar' / 'stepfilled' / 'barstacked' in matplotlib.pylab.hist?

  • Last Update :
  • Techknowledgy :

Compare your result to what happens when you combine the datasets:

import numpy as np
import matplotlib.pylab as plt

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

data = [x1, x2, x3]

fig, ax = plt.subplots(3)

ax[0].hist(data, alpha = 0.3, normed = True, bins = 40)
ax[1].hist(data, histtype = 'stepfilled', alpha = 0.3, normed = True, bins = 40)
ax[2].hist(data, histtype = 'barstacked', alpha = 0.3, normed = True, bins = 40)

plt.show()

Suggestion : 2

Histogram with custom and unequal bin widths.,Selecting different bin counts and sizes can significantly affect the shape of a histogram. The Astropy docs have a great section on how to select these parameters: http://docs.astropy.org/en/stable/visualization/histogram.html,Histogram with step curve with no fill.,Histogram with step curve that has a color fill.

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(19680801)

mu_x = 200
sigma_x = 25
x = np.random.normal(mu_x, sigma_x, size = 100)

mu_w = 200
sigma_w = 10
w = np.random.normal(mu_w, sigma_w, size = 100)

fig, axs = plt.subplots(nrows = 2, ncols = 2)

axs[0, 0].hist(x, 20, density = True, histtype = 'stepfilled', facecolor = 'g',
   alpha = 0.75)
axs[0, 0].set_title('stepfilled')

axs[0, 1].hist(x, 20, density = True, histtype = 'step', facecolor = 'g',
   alpha = 0.75)
axs[0, 1].set_title('step')

axs[1, 0].hist(x, density = True, histtype = 'barstacked', rwidth = 0.8)
axs[1, 0].hist(w, density = True, histtype = 'barstacked', rwidth = 0.8)
axs[1, 0].set_title('barstacked')

# Create a histogram by providing the bin edges(unequally spaced).
bins = [100, 150, 180, 195, 205, 220, 250, 300]
axs[1, 1].hist(x, bins, density = True, histtype = 'bar', rwidth = 0.8)
axs[1, 1].set_title('bar, unequal bins')

fig.tight_layout()
plt.show()

Suggestion : 3

The reason is that histtype only applies anycodings_matplotlib when you pass multiple sets of data to anycodings_matplotlib hist, but you have made 9 separate calls anycodings_matplotlib to hist with one dataset each.,Getting used to plt.hist. However, I see no anycodings_python differences between histtype='bar' / anycodings_python 'stepfilled' / 'barstacked'. This is my code anycodings_python in trial,Compare your result to what happens when anycodings_matplotlib you combine the datasets:,and this is the result which is simply no anycodings_python differences

Getting used to plt.hist. However, I see no anycodings_python differences between histtype='bar' / anycodings_python 'stepfilled' / 'barstacked'. This is my code anycodings_python in trial

import numpy as np
import matplotlib.pylab as plt

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

fig, ax = plt.subplots(3)
kwargs = dict(alpha = 0.3, normed = True, bins = 40)
ax[0].hist(x1, ** kwargs)
ax[0].hist(x2, ** kwargs)
ax[0].hist(x3, ** kwargs)

kwargs1 = dict(histtype = 'stepfilled', alpha = 0.3, normed = True, bins = 40)
ax[1].hist(x1, ** kwargs1)
ax[1].hist(x2, ** kwargs1)
ax[1].hist(x3, ** kwargs1)

kwargs2 = dict(histtype = 'barstacked', alpha = 0.3, normed = True, bins = 40)
ax[2].hist(x1, ** kwargs2)
ax[2].hist(x2, ** kwargs2)
ax[2].hist(x3, ** kwargs2)

plt.show()

Compare your result to what happens when anycodings_matplotlib you combine the datasets:

import numpy as np
import matplotlib.pylab as plt

x1 = np.random.normal(0, 0.8, 1000)
x2 = np.random.normal(-2, 1, 1000)
x3 = np.random.normal(3, 2, 1000)

data = [x1, x2, x3]

fig, ax = plt.subplots(3)

ax[0].hist(data, alpha = 0.3, normed = True, bins = 40)
ax[1].hist(data, histtype = 'stepfilled', alpha = 0.3, normed = True, bins = 40)
ax[2].hist(data, histtype = 'barstacked', alpha = 0.3, normed = True, bins = 40)

plt.show()