Skip to content

Commit 294bd07

Browse files
authored
Merge pull request matplotlib#20457 from dstansby/hist-ex-vars
Rename data variables in histogram example.
2 parents 3fc3b0d + e60bf86 commit 294bd07

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

examples/statistics/hist.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
Histograms
44
==========
55
6-
Demonstrates how to plot histograms with matplotlib.
6+
How to plot histograms with Matplotlib.
77
"""
88

99
import matplotlib.pyplot as plt
1010
import numpy as np
1111
from matplotlib import colors
1212
from matplotlib.ticker import PercentFormatter
1313

14-
# Fixing random state for reproducibility
15-
np.random.seed(19680801)
16-
14+
# Create a random number generator with a fixed seed for reproducibility
15+
rng = np.random.default_rng(19680801)
1716

1817
###############################################################################
1918
# Generate data and plot a simple histogram
@@ -26,15 +25,15 @@
2625
N_points = 100000
2726
n_bins = 20
2827

29-
# Generate a normal distribution, center at x=0 and y=5
30-
x = np.random.randn(N_points)
31-
y = .4 * x + np.random.randn(100000) + 5
28+
# Generate two normal distributions
29+
dist1 = rng.standard_normal(N_points)
30+
dist2 = 0.4 * rng.standard_normal(N_points) + 5
3231

3332
fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True)
3433

3534
# We can set the number of bins with the *bins* keyword argument.
36-
axs[0].hist(x, bins=n_bins)
37-
axs[1].hist(y, bins=n_bins)
35+
axs[0].hist(dist1, bins=n_bins)
36+
axs[1].hist(dist2, bins=n_bins)
3837

3938

4039
###############################################################################
@@ -49,7 +48,7 @@
4948
fig, axs = plt.subplots(1, 2, tight_layout=True)
5049

5150
# N is the count in each bin, bins is the lower-limit of the bin
52-
N, bins, patches = axs[0].hist(x, bins=n_bins)
51+
N, bins, patches = axs[0].hist(dist1, bins=n_bins)
5352

5453
# We'll color code by height, but you could use any scalar
5554
fracs = N / N.max()
@@ -63,7 +62,7 @@
6362
thispatch.set_facecolor(color)
6463

6564
# We can also normalize our inputs by the total number of counts
66-
axs[1].hist(x, bins=n_bins, density=True)
65+
axs[1].hist(dist1, bins=n_bins, density=True)
6766

6867
# Now we format the y-axis to display percentage
6968
axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=1))
@@ -77,7 +76,7 @@
7776
# corresponding to each axis of the histogram.
7877

7978
fig, ax = plt.subplots(tight_layout=True)
80-
hist = ax.hist2d(x, y)
79+
hist = ax.hist2d(dist1, dist2)
8180

8281

8382
###############################################################################
@@ -91,13 +90,13 @@
9190
tight_layout=True)
9291

9392
# We can increase the number of bins on each axis
94-
axs[0].hist2d(x, y, bins=40)
93+
axs[0].hist2d(dist1, dist2, bins=40)
9594

9695
# As well as define normalization of the colors
97-
axs[1].hist2d(x, y, bins=40, norm=colors.LogNorm())
96+
axs[1].hist2d(dist1, dist2, bins=40, norm=colors.LogNorm())
9897

9998
# We can also define custom numbers of bins for each axis
100-
axs[2].hist2d(x, y, bins=(80, 10), norm=colors.LogNorm())
99+
axs[2].hist2d(dist1, dist2, bins=(80, 10), norm=colors.LogNorm())
101100

102101
plt.show()
103102

0 commit comments

Comments
 (0)