From eba8ab1199c17e298d47a9325a01e3de21eb791c Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Wed, 8 Mar 2017 14:43:44 -0800 Subject: [PATCH 1/3] combining and updating 2d hist examples --- examples/pylab_examples/hist2d_demo.py | 43 ++++++++++++++++++++-- examples/pylab_examples/hist2d_log_demo.py | 15 -------- 2 files changed, 39 insertions(+), 19 deletions(-) delete mode 100644 examples/pylab_examples/hist2d_log_demo.py diff --git a/examples/pylab_examples/hist2d_demo.py b/examples/pylab_examples/hist2d_demo.py index 7e4bcf0cfc93..44b9e7af6374 100644 --- a/examples/pylab_examples/hist2d_demo.py +++ b/examples/pylab_examples/hist2d_demo.py @@ -1,13 +1,48 @@ +""" +============= +2D Histograms +============= + +Demonstrates how to plot 2-dimensional histograms. +""" + import matplotlib.pyplot as plt import numpy as np +from matplotlib.colors import LogNorm + +############################################################################### +# Plot a 2D histogram +# ------------------- +# +# To plot a 2D histogram, one only needs two vectors of the same length, +# corresponding to each axis of the histogram. # Fixing random state for reproducibility np.random.seed(19680801) +# Generate a normal distribution, center at x=0 and y=5 +x = np.random.randn(100000) +y = .4 * x + np.random.randn(100000) + 5 + +fig, ax = plt.subplots() +hist = ax.hist2d(x, y) + +############################################################################### +# Customizing your histogram +# -------------------------- +# +# Customizing a 2D histogram is similar to the 1D case, you can control +# visual components such as the bin size or color normalization + +fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True) + +# We can increase the number of bins on each axis +axs[0].hist2d(x, y, bins=40) + +# As well as define normalization of the colors +axs[1].hist2d(x, y, bins=40, norm=LogNorm()) -x = np.random.randn(1000) -y = np.random.randn(1000) + 5 +# We can also define custom numbers of bins for each axis +axs[2].hist2d(x, y, bins=(80, 10), norm=LogNorm()) -# normal distribution center at x=0 and y=5 -plt.hist2d(x, y, bins=40) plt.show() diff --git a/examples/pylab_examples/hist2d_log_demo.py b/examples/pylab_examples/hist2d_log_demo.py deleted file mode 100644 index 22fcbb319826..000000000000 --- a/examples/pylab_examples/hist2d_log_demo.py +++ /dev/null @@ -1,15 +0,0 @@ -from matplotlib.colors import LogNorm -import matplotlib.pyplot as plt -import numpy as np - -# Fixing random state for reproducibility -np.random.seed(19680801) - - -# normal distribution center at x=0 and y=5 -x = np.random.randn(100000) -y = np.random.randn(100000) + 5 - -plt.hist2d(x, y, bins=40, norm=LogNorm()) -plt.colorbar() -plt.show() From 397c76b110d7bb35179e2a4c5ea91ff4d4dbc593 Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Wed, 8 Mar 2017 15:05:09 -0800 Subject: [PATCH 2/3] consolidating histogram examples and moving to SG folder --- examples/pylab_examples/hist2d_demo.py | 48 --------- examples/pylab_examples/hist_colormapped.py | 27 ----- .../pylab_examples/histogram_percent_demo.py | 19 ---- examples/statistics/plot_hist.py | 101 ++++++++++++++++++ examples/tests/backend_driver.py | 1 - lib/matplotlib/axes/_axes.py | 2 +- 6 files changed, 102 insertions(+), 96 deletions(-) delete mode 100644 examples/pylab_examples/hist2d_demo.py delete mode 100644 examples/pylab_examples/hist_colormapped.py delete mode 100644 examples/pylab_examples/histogram_percent_demo.py create mode 100644 examples/statistics/plot_hist.py diff --git a/examples/pylab_examples/hist2d_demo.py b/examples/pylab_examples/hist2d_demo.py deleted file mode 100644 index 44b9e7af6374..000000000000 --- a/examples/pylab_examples/hist2d_demo.py +++ /dev/null @@ -1,48 +0,0 @@ -""" -============= -2D Histograms -============= - -Demonstrates how to plot 2-dimensional histograms. -""" - -import matplotlib.pyplot as plt -import numpy as np -from matplotlib.colors import LogNorm - -############################################################################### -# Plot a 2D histogram -# ------------------- -# -# To plot a 2D histogram, one only needs two vectors of the same length, -# corresponding to each axis of the histogram. - -# Fixing random state for reproducibility -np.random.seed(19680801) - -# Generate a normal distribution, center at x=0 and y=5 -x = np.random.randn(100000) -y = .4 * x + np.random.randn(100000) + 5 - -fig, ax = plt.subplots() -hist = ax.hist2d(x, y) - -############################################################################### -# Customizing your histogram -# -------------------------- -# -# Customizing a 2D histogram is similar to the 1D case, you can control -# visual components such as the bin size or color normalization - -fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True) - -# We can increase the number of bins on each axis -axs[0].hist2d(x, y, bins=40) - -# As well as define normalization of the colors -axs[1].hist2d(x, y, bins=40, norm=LogNorm()) - -# We can also define custom numbers of bins for each axis -axs[2].hist2d(x, y, bins=(80, 10), norm=LogNorm()) - -plt.show() diff --git a/examples/pylab_examples/hist_colormapped.py b/examples/pylab_examples/hist_colormapped.py deleted file mode 100644 index 490eaeeba3f2..000000000000 --- a/examples/pylab_examples/hist_colormapped.py +++ /dev/null @@ -1,27 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt -import matplotlib.cm as cm -import matplotlib.colors as colors - -# Fixing random state for reproducibility -np.random.seed(19680801) - - -fig, ax = plt.subplots() -Ntotal = 1000 -N, bins, patches = ax.hist(np.random.rand(Ntotal), 20) - -# I'll color code by height, but you could use any scalar - - -# we need to normalize the data to 0..1 for the full -# range of the colormap -fracs = N.astype(float)/N.max() -norm = colors.Normalize(fracs.min(), fracs.max()) - -for thisfrac, thispatch in zip(fracs, patches): - color = cm.viridis(norm(thisfrac)) - thispatch.set_facecolor(color) - - -plt.show() diff --git a/examples/pylab_examples/histogram_percent_demo.py b/examples/pylab_examples/histogram_percent_demo.py deleted file mode 100644 index ce62b0a16017..000000000000 --- a/examples/pylab_examples/histogram_percent_demo.py +++ /dev/null @@ -1,19 +0,0 @@ -import matplotlib -from numpy.random import randn -import matplotlib.pyplot as plt -from matplotlib.ticker import PercentFormatter - - -x = randn(5000) - -# Create a figure with some axes. This makes it easier to set the -# formatter later, since that is only available through the OO API. -fig, ax = plt.subplots() - -# Make a normed histogram. It'll be multiplied by 100 later. -ax.hist(x, bins=50, normed=True) - -# Set the formatter. `xmax` sets the value that maps to 100%. -ax.yaxis.set_major_formatter(PercentFormatter(xmax=1)) - -plt.show() diff --git a/examples/statistics/plot_hist.py b/examples/statistics/plot_hist.py new file mode 100644 index 000000000000..0d2756393fc5 --- /dev/null +++ b/examples/statistics/plot_hist.py @@ -0,0 +1,101 @@ +""" +========== +Histograms +========== + +Demonstrates how to plot histograms with matplotlib. +""" + +import matplotlib.pyplot as plt +import numpy as np +from matplotlib import colors +from matplotlib.ticker import PercentFormatter + +# Fixing random state for reproducibility +np.random.seed(19680801) + + +############################################################################### +# Generate data and plot a simple histogram +# ----------------------------------------- +# +# To generate a 1D histogram we only need a single vector of numbers. For a 2D +# histogram we'll need a second vector. We'll generate both below, and show +# the histogram for each vector + +N_points = 100000 +n_bins = 20 + +# Generate a normal distribution, center at x=0 and y=5 +x = np.random.randn(N_points) +y = .4 * x + np.random.randn(100000) + 5 + +fig, axs = plt.subplots(1, 2, sharey=True, tight_layout=True) + +# We can set the number of bins with the `bins` kwarg +axs[0].hist(x, bins=n_bins) +axs[1].hist(y, bins=n_bins) + + +############################################################################### +# Updating histogram colors +# ------------------------- +# +# The histogram method returns (among other things) a `patches` object. This +# gives us access to the properties of the objects drawn. Using this, we can +# edit the histogram to our liking. Let's change the color of each bar +# based on its y value + +fig, axs = plt.subplots(1, 2, figsize=(10, 5), tight_layout=True) + +# N is the count in each bin, bins is the lower-limit of the bin +N, bins, patches = axs[0].hist(x, bins=n_bins) + +# We'll color code by height, but you could use any scalar +fracs = N.astype(float) / N.max() + +# we need to normalize the data to 0..1 for the full range of the colormap +norm = colors.Normalize(fracs.min(), fracs.max()) + +# Now, we'll loop through our objects and set the color of each accordingly +for thisfrac, thispatch in zip(fracs, patches): + color = plt.cm.viridis(norm(thisfrac)) + thispatch.set_facecolor(color) + +# We can also normalize our inputs by the total number of counts. +axs[1].hist(x, bins=n_bins, normed=True) + +# Now we format the y-axis to display percentage +axs[1].yaxis.set_major_formatter(PercentFormatter(xmax=1)) + + +############################################################################### +# Plot a 2D histogram +# ------------------- +# +# To plot a 2D histogram, one only needs two vectors of the same length, +# corresponding to each axis of the histogram. + +fig, ax = plt.subplots(tight_layout=True) +hist = ax.hist2d(x, y) + +############################################################################### +# Customizing your histogram +# -------------------------- +# +# Customizing a 2D histogram is similar to the 1D case, you can control +# visual components such as the bin size or color normalization + +fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True, + tight_layout=True) + +# We can increase the number of bins on each axis +axs[0].hist2d(x, y, bins=40) + +# As well as define normalization of the colors +axs[1].hist2d(x, y, bins=40, norm=colors.LogNorm()) + +# We can also define custom numbers of bins for each axis +axs[2].hist2d(x, y, bins=(80, 10), norm=colors.LogNorm()) + +plt.show() diff --git a/examples/tests/backend_driver.py b/examples/tests/backend_driver.py index 29db836c6f4f..5ad48da75038 100755 --- a/examples/tests/backend_driver.py +++ b/examples/tests/backend_driver.py @@ -173,7 +173,6 @@ 'hatch_demo.py', 'hexbin_demo.py', 'hexbin_demo2.py', - 'hist_colormapped.py', 'vline_hline_demo.py', 'image_clip_path.py', diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index dc7b82386216..2c4f202e1967 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6499,7 +6499,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None, Examples -------- - .. plot:: mpl_examples/pylab_examples/hist2d_demo.py + .. plot:: mpl_examples/statistics/plot_hist.py """ # xrange becomes range after 2to3 From 77c16fb3175897cae8308a688e30756b4d0647ec Mon Sep 17 00:00:00 2001 From: Chris Holdgraf Date: Thu, 9 Mar 2017 20:48:12 -0800 Subject: [PATCH 3/3] addressing small change comments --- examples/statistics/plot_hist.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/statistics/plot_hist.py b/examples/statistics/plot_hist.py index 0d2756393fc5..6e3627a9aeab 100644 --- a/examples/statistics/plot_hist.py +++ b/examples/statistics/plot_hist.py @@ -21,7 +21,7 @@ # # To generate a 1D histogram we only need a single vector of numbers. For a 2D # histogram we'll need a second vector. We'll generate both below, and show -# the histogram for each vector +# the histogram for each vector. N_points = 100000 n_bins = 20 @@ -44,7 +44,7 @@ # The histogram method returns (among other things) a `patches` object. This # gives us access to the properties of the objects drawn. Using this, we can # edit the histogram to our liking. Let's change the color of each bar -# based on its y value +# based on its y value. fig, axs = plt.subplots(1, 2, figsize=(10, 5), tight_layout=True) @@ -62,7 +62,7 @@ color = plt.cm.viridis(norm(thisfrac)) thispatch.set_facecolor(color) -# We can also normalize our inputs by the total number of counts. +# We can also normalize our inputs by the total number of counts axs[1].hist(x, bins=n_bins, normed=True) # Now we format the y-axis to display percentage @@ -79,12 +79,13 @@ fig, ax = plt.subplots(tight_layout=True) hist = ax.hist2d(x, y) + ############################################################################### # Customizing your histogram # -------------------------- # # Customizing a 2D histogram is similar to the 1D case, you can control -# visual components such as the bin size or color normalization +# visual components such as the bin size or color normalization. fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True, tight_layout=True)