Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f767a76

Browse filesBrowse files
committed
Use constant bin widths to make histograms easier to compare visually
1 parent 6ac62c8 commit f767a76
Copy full SHA for f767a76

File tree

Expand file treeCollapse file tree

1 file changed

+6
-3
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+6
-3
lines changed

‎galleries/examples/statistics/histogram_bihistogram.py

Copy file name to clipboardExpand all lines: galleries/examples/statistics/histogram_bihistogram.py
+6-3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.