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 eaf297a

Browse filesBrowse files
authored
Merge pull request #8245 from choldgraf/sg_histograms
DOC: sphinx-gallery histograms
2 parents f885639 + 77c16fb commit eaf297a
Copy full SHA for eaf297a

File tree

Expand file treeCollapse file tree

7 files changed

+103
-76
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+103
-76
lines changed

‎examples/pylab_examples/hist2d_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/hist2d_demo.py
-13Lines changed: 0 additions & 13 deletions
This file was deleted.

‎examples/pylab_examples/hist2d_log_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/hist2d_log_demo.py
-15Lines changed: 0 additions & 15 deletions
This file was deleted.

‎examples/pylab_examples/hist_colormapped.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/hist_colormapped.py
-27Lines changed: 0 additions & 27 deletions
This file was deleted.

‎examples/pylab_examples/histogram_percent_demo.py

Copy file name to clipboardExpand all lines: examples/pylab_examples/histogram_percent_demo.py
-19Lines changed: 0 additions & 19 deletions
This file was deleted.

‎examples/statistics/plot_hist.py

Copy file name to clipboard
+102Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
###############################################################################
84+
# Customizing your histogram
85+
# --------------------------
86+
#
87+
# Customizing a 2D histogram is similar to the 1D case, you can control
88+
# visual components such as the bin size or color normalization.
89+
90+
fig, axs = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True,
91+
tight_layout=True)
92+
93+
# We can increase the number of bins on each axis
94+
axs[0].hist2d(x, y, bins=40)
95+
96+
# As well as define normalization of the colors
97+
axs[1].hist2d(x, y, bins=40, norm=colors.LogNorm())
98+
99+
# We can also define custom numbers of bins for each axis
100+
axs[2].hist2d(x, y, bins=(80, 10), norm=colors.LogNorm())
101+
102+
plt.show()

‎examples/tests/backend_driver.py

Copy file name to clipboardExpand all lines: examples/tests/backend_driver.py
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@
173173
'hatch_demo.py',
174174
'hexbin_demo.py',
175175
'hexbin_demo2.py',
176-
'hist_colormapped.py',
177176
'vline_hline_demo.py',
178177

179178
'image_clip_path.py',

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6499,7 +6499,7 @@ def hist2d(self, x, y, bins=10, range=None, normed=False, weights=None,
64996499
65006500
Examples
65016501
--------
6502-
.. plot:: mpl_examples/pylab_examples/hist2d_demo.py
6502+
.. plot:: mpl_examples/statistics/plot_hist.py
65036503
"""
65046504

65056505
# xrange becomes range after 2to3

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.