Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f767a76

Browse files
committed
Use constant bin widths to make histograms easier to compare visually
1 parent 6ac62c8 commit f767a76

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

galleries/examples/statistics/histogram_bihistogram.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121
# one to be negative. We'll generate data below and plot the bihistogram.
2222

2323
N_points = 10_000
24-
n_bins = 30
2524

2625
# Generate two normal distributions
2726
dataset1 = np.random.normal(0, 1, size=N_points)
2827
dataset2 = np.random.normal(1, 2, size=N_points)
2928

29+
# Use a constant bin width to make the two histograms easier to compare visually
30+
bin_width = 0.25
31+
bins=np.arange(np.min([dataset1, dataset2]), np.max([dataset1, dataset2]) + bin_width, bin_width)
32+
3033
fig, ax = plt.subplots()
3134

3235
# Plot the first histogram
33-
ax.hist(dataset1, bins=n_bins, label="Dataset 1")
36+
ax.hist(dataset1, bins=bins, label="Dataset 1")
3437

3538
# Plot the second histogram
3639
# (notice the negative weights, which flip the histogram upside down)
37-
ax.hist(dataset2, weights=-np.ones_like(dataset2), bins=n_bins, label="Dataset 2")
40+
ax.hist(dataset2, weights=-np.ones_like(dataset2), bins=bins, label="Dataset 2")
3841
ax.axhline(0, color="k")
3942
ax.legend()
4043

0 commit comments

Comments
 (0)