-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathhist.html
More file actions
252 lines (214 loc) · 21.1 KB
/
hist.html
File metadata and controls
252 lines (214 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Histograms — Matplotlib 2.2.2 documentation</title>
<link rel="stylesheet" href="../../_static/mpl.css"
type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css"
type="text/css" />
<link rel="stylesheet" href="../../_static/gallery.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../',
VERSION: '2.2.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../../_static/jquery.js"></script>
<script type="text/javascript" src="../../_static/underscore.js"></script>
<script type="text/javascript" src="../../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Matplotlib 2.2.2 documentation"
href="../../_static/opensearch.xml"/>
<link rel="shortcut icon" href="../../_static/favicon.ico"/>
<link rel="index" title="Index" href="../../genindex.html"
/>
<link rel="search" title="Search" href="../../search.html"
/>
<link rel="top" title="Matplotlib 2.2.2 documentation" href="../../index.html" />
<link rel="canonical" href="https://matplotlib.org/stable/gallery/statistics/hist.html" />
<script data-domain="matplotlib.org" defer="defer" src="https://views.scientific-python.org/js/script.js"></script>
</head>
<body>
<div id="unreleased-message"> You are reading an old version of the documentation (v2.2.2). For the latest version see <a href="https://matplotlib.org/stable/gallery/statistics/hist.html">https://matplotlib.org/stable/gallery/statistics/hist.html</a></div>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px; position: relative;">
<a href="../../index.html">
<div style="float: left; position: absolute; width: 496px; bottom: 0; padding-bottom: 24px"><span style="float: right; color: #789; background: white">Version 2.2.2</span></div>
<img src="../../_static/logo2.png" height="125px" border="0" alt="matplotlib"/></a>
<!-- The "Fork me on github" ribbon -->
<div id="forkongithub"><a href="https://github.com/matplotlib/matplotlib">Fork me on GitHub</a></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li><a href="../../index.html">home</a>| </li>
<li><a href="../index.html">examples</a>| </li>
<li><a href="../../tutorials/index.html">tutorials</a>| </li>
<li><a href="../../api/pyplot_summary.html">pyplot</a>| </li>
<li><a href="../../contents.html">docs</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
<h3><a href="../../contents.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Histograms</a><ul>
<li><a class="reference internal" href="#generate-data-and-plot-a-simple-histogram">Generate data and plot a simple histogram</a></li>
<li><a class="reference internal" href="#updating-histogram-colors">Updating histogram colors</a></li>
<li><a class="reference internal" href="#plot-a-2d-histogram">Plot a 2D histogram</a></li>
<li><a class="reference internal" href="#customizing-your-histogram">Customizing your histogram</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../../contents.html">Documentation overview</a><ul>
</ul></li>
</ul>
</div>
<div id="sidebar-pagesource" role="note" aria-label="source link"
style="margin-top: 1.5em; padding-top: 0.1em; border-top: 1px solid #86989b">
<a href="../../_sources/gallery/statistics/hist.rst.txt"
style="color: #c0c0c0" rel="nofollow">Show Page Source</a>
</div>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="sphx-glr-download-link-note admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Click <a class="reference internal" href="#sphx-glr-download-gallery-statistics-hist-py"><span class="std std-ref">here</span></a> to download the full example code</p>
</div>
<div class="sphx-glr-example-title section" id="histograms">
<span id="sphx-glr-gallery-statistics-hist-py"></span><h1>Histograms<a class="headerlink" href="#histograms" title="Permalink to this headline">¶</a></h1>
<p>Demonstrates how to plot histograms with matplotlib.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">colors</span>
<span class="kn">from</span> <span class="nn">matplotlib.ticker</span> <span class="kn">import</span> <a href="../../api/ticker_api.html#matplotlib.ticker.PercentFormatter" title="View documentation for matplotlib.ticker.PercentFormatter"><span class="n">PercentFormatter</span></a>
<span class="c1"># Fixing random state for reproducibility</span>
<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.seed.html#numpy.random.seed" title="View documentation for numpy.random.seed"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">seed</span></a><span class="p">(</span><span class="mi">19680801</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="generate-data-and-plot-a-simple-histogram">
<h2>Generate data and plot a simple histogram<a class="headerlink" href="#generate-data-and-plot-a-simple-histogram" title="Permalink to this headline">¶</a></h2>
<p>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.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">N_points</span> <span class="o">=</span> <span class="mi">100000</span>
<span class="n">n_bins</span> <span class="o">=</span> <span class="mi">20</span>
<span class="c1"># Generate a normal distribution, center at x=0 and y=5</span>
<span class="n">x</span> <span class="o">=</span> <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html#numpy.random.randn" title="View documentation for numpy.random.randn"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span></a><span class="p">(</span><span class="n">N_points</span><span class="p">)</span>
<span class="n">y</span> <span class="o">=</span> <span class="o">.</span><span class="mi">4</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randn.html#numpy.random.randn" title="View documentation for numpy.random.randn"><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span></a><span class="p">(</span><span class="mi">100000</span><span class="p">)</span> <span class="o">+</span> <span class="mi">5</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axs</span> <span class="o">=</span> <a href="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">sharey</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">tight_layout</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c1"># We can set the number of bins with the `bins` kwarg</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">n_bins</span><span class="p">)</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">n_bins</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../../_images/sphx_glr_hist_001.png" class="sphx-glr-single-img" src="../../_images/sphx_glr_hist_001.png" />
</div>
<div class="section" id="updating-histogram-colors">
<h2>Updating histogram colors<a class="headerlink" href="#updating-histogram-colors" title="Permalink to this headline">¶</a></h2>
<p>The histogram method returns (among other things) a <code class="xref py py-obj docutils literal notranslate"><span class="pre">patches</span></code> 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.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">fig</span><span class="p">,</span> <span class="n">axs</span> <span class="o">=</span> <a href="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">tight_layout</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c1"># N is the count in each bin, bins is the lower-limit of the bin</span>
<span class="n">N</span><span class="p">,</span> <span class="n">bins</span><span class="p">,</span> <span class="n">patches</span> <span class="o">=</span> <span class="n">axs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">n_bins</span><span class="p">)</span>
<span class="c1"># We'll color code by height, but you could use any scalar</span>
<span class="n">fracs</span> <span class="o">=</span> <span class="n">N</span> <span class="o">/</span> <span class="n">N</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="c1"># we need to normalize the data to 0..1 for the full range of the colormap</span>
<span class="n">norm</span> <span class="o">=</span> <a href="../../api/_as_gen/matplotlib.colors.Normalize.html#matplotlib.colors.Normalize" title="View documentation for matplotlib.colors.Normalize"><span class="n">colors</span><span class="o">.</span><span class="n">Normalize</span></a><span class="p">(</span><span class="n">fracs</span><span class="o">.</span><span class="n">min</span><span class="p">(),</span> <span class="n">fracs</span><span class="o">.</span><span class="n">max</span><span class="p">())</span>
<span class="c1"># Now, we'll loop through our objects and set the color of each accordingly</span>
<span class="k">for</span> <span class="n">thisfrac</span><span class="p">,</span> <span class="n">thispatch</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">fracs</span><span class="p">,</span> <span class="n">patches</span><span class="p">):</span>
<span class="n">color</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">cm</span><span class="o">.</span><span class="n">viridis</span><span class="p">(</span><span class="n">norm</span><span class="p">(</span><span class="n">thisfrac</span><span class="p">))</span>
<span class="n">thispatch</span><span class="o">.</span><span class="n">set_facecolor</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
<span class="c1"># We can also normalize our inputs by the total number of counts</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="n">n_bins</span><span class="p">,</span> <span class="n">density</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c1"># Now we format the y-axis to display percentage</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">yaxis</span><span class="o">.</span><span class="n">set_major_formatter</span><span class="p">(</span><a href="../../api/ticker_api.html#matplotlib.ticker.PercentFormatter" title="View documentation for matplotlib.ticker.PercentFormatter"><span class="n">PercentFormatter</span></a><span class="p">(</span><span class="n">xmax</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
</pre></div>
</div>
<img alt="../../_images/sphx_glr_hist_002.png" class="sphx-glr-single-img" src="../../_images/sphx_glr_hist_002.png" />
</div>
<div class="section" id="plot-a-2d-histogram">
<h2>Plot a 2D histogram<a class="headerlink" href="#plot-a-2d-histogram" title="Permalink to this headline">¶</a></h2>
<p>To plot a 2D histogram, one only needs two vectors of the same length,
corresponding to each axis of the histogram.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">fig</span><span class="p">,</span> <span class="n">ax</span> <span class="o">=</span> <a href="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="n">tight_layout</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">hist</span> <span class="o">=</span> <span class="n">ax</span><span class="o">.</span><span class="n">hist2d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../../_images/sphx_glr_hist_003.png" class="sphx-glr-single-img" src="../../_images/sphx_glr_hist_003.png" />
</div>
<div class="section" id="customizing-your-histogram">
<h2>Customizing your histogram<a class="headerlink" href="#customizing-your-histogram" title="Permalink to this headline">¶</a></h2>
<p>Customizing a 2D histogram is similar to the 1D case, you can control
visual components such as the bin size or color normalization.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">fig</span><span class="p">,</span> <span class="n">axs</span> <span class="o">=</span> <a href="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><span class="n">plt</span><span class="o">.</span><span class="n">subplots</span></a><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">figsize</span><span class="o">=</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">15</span><span class="p">),</span> <span class="n">sharex</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">sharey</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
<span class="n">tight_layout</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="c1"># We can increase the number of bins on each axis</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">hist2d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">40</span><span class="p">)</span>
<span class="c1"># As well as define normalization of the colors</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">hist2d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="mi">40</span><span class="p">,</span> <span class="n">norm</span><span class="o">=</span><a href="../../api/_as_gen/matplotlib.colors.LogNorm.html#matplotlib.colors.LogNorm" title="View documentation for matplotlib.colors.LogNorm"><span class="n">colors</span><span class="o">.</span><span class="n">LogNorm</span></a><span class="p">())</span>
<span class="c1"># We can also define custom numbers of bins for each axis</span>
<span class="n">axs</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">hist2d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">bins</span><span class="o">=</span><span class="p">(</span><span class="mi">80</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">norm</span><span class="o">=</span><a href="../../api/_as_gen/matplotlib.colors.LogNorm.html#matplotlib.colors.LogNorm" title="View documentation for matplotlib.colors.LogNorm"><span class="n">colors</span><span class="o">.</span><span class="n">LogNorm</span></a><span class="p">())</span>
<a href="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><span class="n">plt</span><span class="o">.</span><span class="n">show</span></a><span class="p">()</span>
</pre></div>
</div>
<img alt="../../_images/sphx_glr_hist_004.png" class="sphx-glr-single-img" src="../../_images/sphx_glr_hist_004.png" />
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-gallery-statistics-hist-py">
<div class="sphx-glr-download docutils container">
<a class="reference download internal" href="../../_downloads/hist.py" download=""><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">hist.py</span></code></a></div>
<div class="sphx-glr-download docutils container">
<a class="reference download internal" href="../../_downloads/hist.ipynb" download=""><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">hist.ipynb</span></code></a></div>
</div>
<p class="sphx-glr-signature">Keywords: matplotlib code example, codex, python plot, pyplot
<a class="reference external" href="https://sphinx-gallery.readthedocs.io">Gallery generated by Sphinx-Gallery</a></p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the Matplotlib development team; 2012 - 2018 The Matplotlib development team.
<br />
Last updated on Jun 28, 2018.
Created using
<ahref="http://sphinx-doc.org/">Sphinx</a> 1.7.5.
Doc version v2.2.2-101-g15e1eadd0.
</div>
</body>
<footer>
</footer>
</html>