|
3 | 3 | Histograms |
4 | 4 | ========== |
5 | 5 |
|
6 | | -Demonstrates how to plot histograms with matplotlib. |
| 6 | +How to plot histograms with Matplotlib. |
7 | 7 | """ |
8 | 8 |
|
9 | 9 | import matplotlib.pyplot as plt |
10 | 10 | import numpy as np |
11 | 11 | from matplotlib import colors |
12 | 12 | from matplotlib.ticker import PercentFormatter |
13 | 13 |
|
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) |
17 | 16 |
|
18 | 17 | ############################################################################### |
19 | 18 | # Generate data and plot a simple histogram |
|
26 | 25 | N_points = 100000 |
27 | 26 | n_bins = 20 |
28 | 27 |
|
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 |
32 | 31 |
|
33 | 32 | fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) |
34 | 33 |
|
35 | 34 | # 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) |
38 | 37 |
|
39 | 38 |
|
40 | 39 | ############################################################################### |
|
49 | 48 | fig, axs = plt.subplots(1, 2, tight_layout=True) |
50 | 49 |
|
51 | 50 | # 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) |
53 | 52 |
|
54 | 53 | # We'll color code by height, but you could use any scalar |
55 | 54 | fracs = N / N.max() |
|
63 | 62 | thispatch.set_facecolor(color) |
64 | 63 |
|
65 | 64 | # 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) |
67 | 66 |
|
68 | 67 | # Now we format the y-axis to display percentage |
69 | 68 | axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=1)) |
|
77 | 76 | # corresponding to each axis of the histogram. |
78 | 77 |
|
79 | 78 | fig, ax = plt.subplots(tight_layout=True) |
80 | | -hist = ax.hist2d(x, y) |
| 79 | +hist = ax.hist2d(dist1, dist2) |
81 | 80 |
|
82 | 81 |
|
83 | 82 | ############################################################################### |
|
91 | 90 | tight_layout=True) |
92 | 91 |
|
93 | 92 | # 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) |
95 | 94 |
|
96 | 95 | # 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()) |
98 | 97 |
|
99 | 98 | # 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()) |
101 | 100 |
|
102 | 101 | plt.show() |
103 | 102 |
|
|
0 commit comments