| [ Web Proxy ] |
| Viewing: https://matplotlib.org/gallery/mplot3d/../user_interfaces/../statistics/histogram_bihistogram.html | [Back] [Original] |
Section Navigation
fill_between with transparencyhisttype settingsplt.subplotsfloating_axes featuresNote
Go to the end to download the full example code.
How to plot a bihistogram with Matplotlib.
import matplotlib.pyplot as plt
import numpy as np
# Create a random number generator with a fixed seed for reproducibility
rng = np.random.default_rng(19680801)
To generate a bihistogram we need two datasets (each being a vector of numbers). We will plot both histograms using plt.hist() and set the weights of the second one to be negative. We'll generate data below and plot the bihistogram.
N_points = 10_000
# Generate two normal distributions
dataset1 = np.random.normal(0, 1, size=N_points)
dataset2 = np.random.normal(1, 2, size=N_points)
# Use a constant bin width to make the two histograms easier to compare visually
bin_width = 0.25
bins = np.arange(np.min([dataset1, dataset2]),
np.max([dataset1, dataset2]) + bin_width, bin_width)
fig, ax = plt.subplots()
# Plot the first histogram
ax.hist(dataset1, bins=bins, label="Dataset 1")
# Plot the second histogram
# (notice the negative weights, which flip the histogram upside down)
ax.hist(dataset2, weights=-np.ones_like(dataset2), bins=bins, label="Dataset 2")
ax.axhline(0, color="k")
ax.legend()
plt.show()
[histogram bihistogram]Tags: plot-type: histogram domain: statistics purpose: showcase
Copyright 20022012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 20122026 The Matplotlib development team.
Created using Sphinx 9.1.0.
Built from v3.11.2-1-g879ad8ebba.
Built with the PyData Sphinx Theme 0.16.1.
| Web Proxy Viewer | New URL | Original Page |