|
| 1 | +""" |
| 2 | +========== |
| 3 | +Histograms |
| 4 | +========== |
| 5 | +
|
| 6 | +Demonstrates how to plot histograms with matplotlib. |
| 7 | +""" |
| 8 | + |
| 9 | +import matplotlib.pyplot as plt |
| 10 | +import numpy as np |
| 11 | +from matplotlib import colors |
| 12 | +from matplotlib.ticker import PercentFormatter |
| 13 | + |
| 14 | +# Fixing random state for reproducibility |
| 15 | +np.random.seed(19680801) |
| 16 | + |
| 17 | + |
| 18 | +############################################################################### |
| 19 | +# Generate data and plot a simple histogram |
| 20 | +# ----------------------------------------- |
| 21 | +# |
| 22 | +# To generate a 1D histogram we only need a single vector of numbers. For a 2D |
| 23 | +# histogram we'll need a second vector. We'll generate both below, and show |
| 24 | +# the histogram for each vector |
| 25 | + |
| 26 | +N_points = 100000 |
| 27 | +n_bins = 20 |
| 28 | + |
| 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 |
| 32 | + |
| 33 | +fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) |
| 34 | + |
| 35 | +# We can set the number of bins with the `bins` kwarg |
| 36 | +axs[0].hist(x, bins=n_bins) |
| 37 | +axs[1].hist(y, bins=n_bins) |
| 38 | + |
| 39 | + |
| 40 | +############################################################################### |
| 41 | +# Updating histogram colors |
| 42 | +# ------------------------- |
| 43 | +# |
| 44 | +# The histogram method returns (among other things) a `patches` object. This |
| 45 | +# gives us access to the properties of the objects drawn. Using this, we can |
| 46 | +# edit the histogram to our liking. Let's change the color of each bar |
| 47 | +# based on its y value |
| 48 | + |
| 49 | +fig, axs = plt.subplots(1, 2, figsize=(10, 5), tight_layout=True) |
| 50 | + |
| 51 | +# 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) |
| 53 | + |
| 54 | +# We'll color code by height, but you could use any scalar |
| 55 | +fracs = N.astype(float) / N.max() |
| 56 | + |
| 57 | +# we need to normalize the data to 0..1 for the full range of the colormap |
| 58 | +norm = colors.Normalize(fracs.min(), fracs.max()) |
| 59 | + |
| 60 | +# Now, we'll loop through our objects and set the color of each accordingly |
| 61 | +for thisfrac, thispatch in zip(fracs, patches): |
| 62 | + color = plt.cm.viridis(norm(thisfrac)) |
| 63 | + thispatch.set_facecolor(color) |
| 64 | + |
| 65 | +# We can also normalize our inputs by the total number of counts. |
| 66 | +axs[1].hist(x, bins=n_bins, normed=True) |
| 67 | + |
| 68 | +# Now we format the y-axis to display percentage |
| 69 | +axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=1)) |
| 70 | + |
| 71 | + |
| 72 | +############################################################################### |
| 73 | +# Plot a 2D histogram |
| 74 | +# ------------------- |
| 75 | +# |
| 76 | +# To plot a 2D histogram, one only needs two vectors of the same length, |
| 77 | +# corresponding to each axis of the histogram. |
| 78 | + |
| 79 | +fig, ax = plt.subplots(tight_layout=True) |
| 80 | +hist = ax.hist2d(x, y) |
| 81 | + |
| 82 | +############################################################################### |
| 83 | +# Customizing your histogram |
| 84 | +# -------------------------- |
| 85 | +# |
| 86 | +# Customizing a 2D histogram is similar to the 1D case, you can control |
| 87 | +# visual components such as the bin size or color normalization |
| 88 | + |
| 89 | +fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True, |
| 90 | + tight_layout=True) |
| 91 | + |
| 92 | +# We can increase the number of bins on each axis |
| 93 | +axs[0].hist2d(x, y, bins=40) |
| 94 | + |
| 95 | +# As well as define normalization of the colors |
| 96 | +axs[1].hist2d(x, y, bins=40, norm=colors.LogNorm()) |
| 97 | + |
| 98 | +# We can also define custom numbers of bins for each axis |
| 99 | +axs[2].hist2d(x, y, bins=(80, 10), norm=colors.LogNorm()) |
| 100 | + |
| 101 | +plt.show() |
0 commit comments