-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathunits.html
More file actions
313 lines (259 loc) · 21.6 KB
/
units.html
File metadata and controls
313 lines (259 loc) · 21.6 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!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>matplotlib.units — Matplotlib 3.1.2 documentation</title>
<link rel="stylesheet" href="../../_static/mpl.css?v3.1.1-79-g90d53b526"
type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css"
type="text/css" />
<link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" />
<link rel="stylesheet" href="../../_static/gallery.css" type="text/css" />
<link rel="stylesheet" href="../../_static/copybutton.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></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>
<script type="text/javascript" src="../../_static/language_data.js"></script>
<script type="text/javascript" src="../../_static/clipboard.min.js"></script>
<script type="text/javascript" src="../../_static/copybutton.js"></script>
<script type="text/javascript">var copybuttonSkipText = '>>> ';</script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Matplotlib 3.1.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 3.1.2 documentation" href="../../index.html" />
<link rel="up" title="matplotlib" href="../matplotlib.html" />
<link rel="canonical" href="https://matplotlib.org/3.4.3/_modules/matplotlib/units.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 (v3.1.1). For the latest version see <a href="/stable/">https://matplotlib.org/stable/</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 3.1.2</span></div>
<img src="../../_static/logo2_compressed.svg" 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>
<nav class="main-nav">
<ul>
<li><a href="../../users/installing.html">Installation</a></li>
<li><a href="../../contents.html">Documentation</a></li>
<li><a href="../../gallery/index.html">Examples</a></li>
<li><a href="../../tutorials/index.html">Tutorials</a></li>
<li><a href="../../devel/index.html">Contributing</a></li>
</ul>
</nav>
<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="../../contents.html">contents</a> »</li>
<li><a href="../index.html" >Module code</a> »</li>
<li><a href="../matplotlib.html" accesskey="U">matplotlib</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><div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../../contents.html">Documentation overview</a><ul>
<li><a href="../index.html">Module code</a><ul>
<li><a href="../matplotlib.html">matplotlib</a><ul>
</ul></li>
</ul></li>
</ul></li>
</ul>
</div>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<h1>Source code for matplotlib.units</h1><div class="highlight"><pre>
<span></span><span class="sd">"""</span>
<span class="sd">The classes here provide support for using custom classes with</span>
<span class="sd">Matplotlib, e.g., those that do not expose the array interface but know</span>
<span class="sd">how to convert themselves to arrays. It also supports classes with</span>
<span class="sd">units and units conversion. Use cases include converters for custom</span>
<span class="sd">objects, e.g., a list of datetime objects, as well as for objects that</span>
<span class="sd">are unit aware. We don't assume any particular units implementation;</span>
<span class="sd">rather a units implementation must provide the register with the Registry</span>
<span class="sd">converter dictionary and a `ConversionInterface`. For example,</span>
<span class="sd">here is a complete implementation which supports plotting with native</span>
<span class="sd">datetime objects::</span>
<span class="sd"> import matplotlib.units as units</span>
<span class="sd"> import matplotlib.dates as dates</span>
<span class="sd"> import matplotlib.ticker as ticker</span>
<span class="sd"> import datetime</span>
<span class="sd"> class DateConverter(units.ConversionInterface):</span>
<span class="sd"> @staticmethod</span>
<span class="sd"> def convert(value, unit, axis):</span>
<span class="sd"> 'Convert a datetime value to a scalar or array'</span>
<span class="sd"> return dates.date2num(value)</span>
<span class="sd"> @staticmethod</span>
<span class="sd"> def axisinfo(unit, axis):</span>
<span class="sd"> 'Return major and minor tick locators and formatters'</span>
<span class="sd"> if unit!='date': return None</span>
<span class="sd"> majloc = dates.AutoDateLocator()</span>
<span class="sd"> majfmt = dates.AutoDateFormatter(majloc)</span>
<span class="sd"> return AxisInfo(majloc=majloc,</span>
<span class="sd"> majfmt=majfmt,</span>
<span class="sd"> label='date')</span>
<span class="sd"> @staticmethod</span>
<span class="sd"> def default_units(x, axis):</span>
<span class="sd"> 'Return the default unit for x or None'</span>
<span class="sd"> return 'date'</span>
<span class="sd"> # Finally we register our object type with the Matplotlib units registry.</span>
<span class="sd"> units.registry[datetime.date] = DateConverter()</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">numbers</span> <span class="kn">import</span> <span class="n">Number</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">ma</span>
<span class="kn">from</span> <span class="nn">matplotlib</span> <span class="kn">import</span> <span class="n">cbook</span>
<div class="viewcode-block" id="ConversionError"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionError">[docs]</a><span class="k">class</span> <span class="nc">ConversionError</span><span class="p">(</span><span class="ne">TypeError</span><span class="p">):</span>
<span class="k">pass</span></div>
<div class="viewcode-block" id="AxisInfo"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.AxisInfo">[docs]</a><span class="k">class</span> <span class="nc">AxisInfo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Information to support default axis labeling, tick labeling, and limits.</span>
<span class="sd"> An instance of this class must be returned by</span>
<span class="sd"> `ConversionInterface.axisinfo`.</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">majloc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">minloc</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">majfmt</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">minfmt</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
<span class="n">default_limits</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Parameters</span>
<span class="sd"> ----------</span>
<span class="sd"> majloc, minloc : Locator, optional</span>
<span class="sd"> Tick locators for the major and minor ticks.</span>
<span class="sd"> majfmt, minfmt : Formatter, optional</span>
<span class="sd"> Tick formatters for the major and minor ticks.</span>
<span class="sd"> label : str, optional</span>
<span class="sd"> The default axis label.</span>
<span class="sd"> default_limits : optional</span>
<span class="sd"> The default min and max limits of the axis if no data has</span>
<span class="sd"> been plotted.</span>
<span class="sd"> Notes</span>
<span class="sd"> -----</span>
<span class="sd"> If any of the above are ``None``, the axis will simply use the</span>
<span class="sd"> default value.</span>
<span class="sd"> """</span>
<span class="bp">self</span><span class="o">.</span><span class="n">majloc</span> <span class="o">=</span> <span class="n">majloc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">minloc</span> <span class="o">=</span> <span class="n">minloc</span>
<span class="bp">self</span><span class="o">.</span><span class="n">majfmt</span> <span class="o">=</span> <span class="n">majfmt</span>
<span class="bp">self</span><span class="o">.</span><span class="n">minfmt</span> <span class="o">=</span> <span class="n">minfmt</span>
<span class="bp">self</span><span class="o">.</span><span class="n">label</span> <span class="o">=</span> <span class="n">label</span>
<span class="bp">self</span><span class="o">.</span><span class="n">default_limits</span> <span class="o">=</span> <span class="n">default_limits</span></div>
<div class="viewcode-block" id="ConversionInterface"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionInterface">[docs]</a><span class="k">class</span> <span class="nc">ConversionInterface</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> The minimal interface for a converter to take custom data types (or</span>
<span class="sd"> sequences) and convert them to values Matplotlib can use.</span>
<span class="sd"> """</span>
<div class="viewcode-block" id="ConversionInterface.axisinfo"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionInterface.axisinfo">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">axisinfo</span><span class="p">(</span><span class="n">unit</span><span class="p">,</span> <span class="n">axis</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return an `~units.AxisInfo` for the axis with the specified units.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="kc">None</span></div>
<div class="viewcode-block" id="ConversionInterface.default_units"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionInterface.default_units">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">default_units</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">axis</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Return the default unit for *x* or ``None`` for the given axis.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="kc">None</span></div>
<div class="viewcode-block" id="ConversionInterface.convert"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionInterface.convert">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">convert</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="n">unit</span><span class="p">,</span> <span class="n">axis</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> Convert *obj* using *unit* for the specified *axis*.</span>
<span class="sd"> If *obj* is a sequence, return the converted sequence. The output must</span>
<span class="sd"> be a sequence of scalars that can be used by the numpy array layer.</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="n">obj</span></div>
<div class="viewcode-block" id="ConversionInterface.is_numlike"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.ConversionInterface.is_numlike">[docs]</a> <span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">is_numlike</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="sd">"""</span>
<span class="sd"> The Matplotlib datalim, autoscaling, locators etc work with scalars</span>
<span class="sd"> which are the units converted to floats given the current unit. The</span>
<span class="sd"> converter may be passed these floats, or arrays of them, even when</span>
<span class="sd"> units are set.</span>
<span class="sd"> """</span>
<span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">iterable</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">for</span> <span class="n">thisx</span> <span class="ow">in</span> <span class="n">x</span><span class="p">:</span>
<span class="k">if</span> <span class="n">thisx</span> <span class="ow">is</span> <span class="n">ma</span><span class="o">.</span><span class="n">masked</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">thisx</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">Number</span><span class="p">)</span></div></div>
<div class="viewcode-block" id="Registry"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.Registry">[docs]</a><span class="k">class</span> <span class="nc">Registry</span><span class="p">(</span><span class="nb">dict</span><span class="p">):</span>
<span class="sd">"""Register types with conversion interface."""</span>
<div class="viewcode-block" id="Registry.get_converter"><a class="viewcode-back" href="../../api/units_api.html#matplotlib.units.Registry.get_converter">[docs]</a> <span class="k">def</span> <span class="nf">get_converter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="sd">"""Get the converter interface instance for *x*, or None."""</span>
<span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s2">"values"</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">values</span> <span class="c1"># Unpack pandas Series and DataFrames.</span>
<span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
<span class="c1"># In case x in a masked array, access the underlying data (only its</span>
<span class="c1"># type matters). If x is a regular ndarray, getdata() just returns</span>
<span class="c1"># the array itself.</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">getdata</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
<span class="c1"># If there are no elements in x, infer the units from its dtype</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">x</span><span class="o">.</span><span class="n">size</span><span class="p">:</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_converter</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">x</span><span class="o">.</span><span class="n">dtype</span><span class="p">))</span>
<span class="k">try</span><span class="p">:</span> <span class="c1"># Look up in the cache.</span>
<span class="k">return</span> <span class="bp">self</span><span class="p">[</span><span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">)]</span>
<span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span> <span class="c1"># If cache lookup fails, look up based on first element...</span>
<span class="n">first</span> <span class="o">=</span> <span class="n">cbook</span><span class="o">.</span><span class="n">safe_first_element</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="ne">StopIteration</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># ... and avoid infinite recursion for pathological iterables</span>
<span class="c1"># where indexing returns instances of the same iterable class.</span>
<span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">first</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="nb">type</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_converter</span><span class="p">(</span><span class="n">first</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">None</span></div></div>
<span class="n">registry</span> <span class="o">=</span> <span class="n">Registry</span><span class="p">()</span>
</pre></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 Jan 05, 2020.
Created using
<a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
Doc version v3.1.1-79-g90d53b526.
</div>
</body>
<footer>
</footer>
</html>