-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtut_plot.html
254 lines (235 loc) · 22.7 KB
/
tut_plot.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plotting — PyGeode 1.4.1-rc2 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/pygtheme.css" />
<link rel="stylesheet" type="text/css" href="_static/plot_directive.css" />
<link rel="stylesheet" type="text/css" href="_static/sg_gallery.css" />
<link rel="stylesheet" type="text/css" href="_static/sg_gallery-binder.css" />
<link rel="stylesheet" type="text/css" href="_static/sg_gallery-dataframe.css" />
<link rel="stylesheet" type="text/css" href="_static/sg_gallery-rendered-html.css" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/pygeode_icon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Working with Axes" href="tut_axes.html" />
<link rel="prev" title="Variable input and output" href="tut_io.html" />
<link href="http://fonts.googleapis.com/css?family=Ubuntu:300,300italic,regular,italic,500,500italic,bold,bolditalic" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700' rel='stylesheet' type='text/css'>
</head><body>
<div class="header" role="banner"><img class="logo" src="_static/pygeode_logo.png" width=79px alt="Logo"/>
<h1 class="heading"><a href="index.html">
<span>PyGeode 1.4.1-rc2 documentation</span></a></h1>
<h2 class="heading"><span>Plotting</span></h2>
</div>
<div class="topnav" role="navigation" aria-label="top navigation">
<p>
«  <a href="tut_io.html">Variable input and output</a>
  ::  
<a class="uplink" href="reference.html">Reference</a>
  ::  
<a class="uplink" href="tutorial.html">Tutorial</a>
  ::  
<a class="uplink" href="gallery/index.html">Gallery</a>
  ::  
<a href="tut_axes.html">Working with Axes</a>  »
</p>
</div>
<div class="content">
<div class="section" id="plotting">
<h1>Plotting<a class="headerlink" href="#plotting" title="Permalink to this headline">¶</a></h1>
<p>The plotting system in PyGeode is intended to provide sensible defaults so that
you can quickly see a reasonably annotated plot of the contents of a PyGeode
variable, but also to allow as much flexibility in customizing your plots as
possible. In general it is intended to map very closely to the matplotlib
library, and to give you access to as much of the matplotlib functionality as
possible. This tutorial is intended to introduce some of the basic concepts; a
gallery is also available with sample plots. Though you could use either
document as a starting point for plotting even without any matplotlib
experience, both will make more sense if you have had some.</p>
<p>There are three levels of plotting commands. The highest levels, mostly
prefixed by <code class="docutils literal notranslate"><span class="pre">show</span></code>, make the most assumptions about the structure of the plot
they produce. The next level, prefixed with <code class="docutils literal notranslate"><span class="pre">v</span></code>, are more direct wrappers
around matplotlib commands that are aware of PyGeode variables and try to make
formatting decisions accordingly. The lowest level are very thin wrappers
around individual matplotlib commands that know nothing about PyGeode
variables, but allow for the most direct control. In the end the plots that are
produced are matplotlib plots, so any level of customization that matplotlib is
capable of can be achieved.</p>
<p>The basic high-level plotting command is <a class="reference internal" href="plot.html#pygeode.showvar" title="pygeode.showvar"><code class="xref py py-func docutils literal notranslate"><span class="pre">showvar()</span></code></a>, which in its
simplest form will plot a one or two dimensional variable:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kn">import</span> <span class="nn">pygeode</span> <span class="k">as</span> <span class="nn">pyg</span><span class="p">;</span> <span class="kn">from</span> <span class="nn">pygeode.tutorial</span> <span class="kn">import</span> <span class="n">t1</span><span class="p">,</span> <span class="n">t2</span>
<span class="go"># A line plot</span>
<span class="gp">In [2]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t1</span><span class="o">.</span><span class="n">Temp</span><span class="p">(</span><span class="n">lat</span><span class="o">=</span><span class="mi">45</span><span class="p">))</span>
<span class="gh">Out[2]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727ce3670></span>
<span class="go"># A contour plot</span>
<span class="gp">In [3]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t1</span><span class="o">.</span><span class="n">Temp</span><span class="p">)</span>
<span class="gh">Out[3]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f37341cac40></span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/showvar1d.png"><img alt="_images/showvar1d.png" src="_images/showvar1d.png" style="width: 4in;" /></a>
<a class="reference internal image-reference" href="_images/showvar2d.png"><img alt="_images/showvar2d.png" src="_images/showvar2d.png" style="width: 4in;" /></a>
<p>The axes are labelled and formatted according to behaviour set by the axis
objects (explained in more detail below), and the title is set from the
variable name as well as the coordinates of any degenerate axes. In the case of
the contour plots, a colorbar is added automatically.</p>
<div class="section" id="showvar-line-plots">
<h2>Showvar: Line Plots<a class="headerlink" href="#showvar-line-plots" title="Permalink to this headline">¶</a></h2>
<p>If <a class="reference internal" href="plot.html#pygeode.showvar" title="pygeode.showvar"><code class="xref py py-func docutils literal notranslate"><span class="pre">showvar()</span></code></a> is passed a one dimensional variable, it will produce a
line plot (ultimately using <code class="xref py py-func docutils literal notranslate"><span class="pre">matplotlib.plot()</span></code>), automatically extracting
the coordinates and values from the variable. If the axis is recognized by
PyGeode as being a vertical axis, the plot will be transposed. This can be
reversed if desired by setting the keyword argument <code class="docutils literal notranslate"><span class="pre">transpose</span> <span class="pre">=</span> <span class="pre">True</span></code>.</p>
<p>Like the plot command, the format of the line can be set by passing a string
immediately following the command; moreover, any other keyword argument
recognized by <code class="xref py py-func docutils literal notranslate"><span class="pre">matplotlib.plot()</span></code> will be passed through.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="go"># A vertical plot</span>
<span class="gp">In [4]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">Temp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">,</span> <span class="s1">'lat'</span><span class="p">,</span> <span class="s1">'lon'</span><span class="p">),</span> <span class="s1">'k--'</span><span class="p">,</span> <span class="n">lw</span><span class="o">=</span><span class="mf">3.</span><span class="p">)</span>
<span class="gh">Out[4]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f373400a430></span>
<span class="go"># A transposed plot</span>
<span class="gp">In [5]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">Temp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">,</span> <span class="s1">'lat'</span><span class="p">,</span> <span class="s1">'lon'</span><span class="p">),</span> <span class="n">transpose</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span>
<span class="gh">Out[5]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727a824c0></span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/lp_vertical.png"><img alt="_images/lp_vertical.png" src="_images/lp_vertical.png" style="width: 4in;" /></a>
<a class="reference internal image-reference" href="_images/lp_transpose.png"><img alt="_images/lp_transpose.png" src="_images/lp_transpose.png" style="width: 4in;" /></a>
</div>
<div class="section" id="showvar-contour-plots">
<h2>Showvar: Contour Plots<a class="headerlink" href="#showvar-contour-plots" title="Permalink to this headline">¶</a></h2>
<p>The showvar command, if passed a two-dimensional variable, will also produce
contour plots. If no arguments (beyond the variable to be plotted), a set of
(filled) contour intervals and an appropriate colourmap is chosen based on
whether it thinks the data is better presented as a ‘divergent’ quantity
centred on zero, or a ‘sequential’ quantity; different algorithms for choosing
the contour interval are used in each case.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="go"># Sequential data</span>
<span class="gp">In [6]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">Temp</span><span class="p">(</span><span class="n">pres</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">))</span>
<span class="gh">Out[6]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727d41430></span>
<span class="go"># Divergent data</span>
<span class="gp">In [7]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">U</span><span class="p">(</span><span class="n">pres</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">))</span>
<span class="gh">Out[7]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727944fd0></span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/cp_sequential.png"><img alt="_images/cp_sequential.png" src="_images/cp_sequential.png" style="width: 4in;" /></a>
<a class="reference internal image-reference" href="_images/cp_divergent.png"><img alt="_images/cp_divergent.png" src="_images/cp_divergent.png" style="width: 4in;" /></a>
<p>PyGeode tries to choose a reasonably simple contour interval and sets the
colourmap and tick labeling accordingly. Since the temperature data is centred
away from zero, PyGeode assumes this is better plotted with a ‘sequential’
colourmap, with gradients tending in a single direction. However, the wind data
is centred around zero, so the contour intervals chosen are also centred around
zero with a colourmap that emphasizes oppositely signed values.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="go"># Create a sequential style colormap with three divisions, three</span>
<span class="go"># contour lines and six filled intervals per division</span>
<span class="gp">In [8]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">U</span><span class="p">(</span><span class="n">pres</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">),</span> <span class="n">style</span><span class="o">=</span><span class="s1">'seq'</span><span class="p">,</span> <span class="n">ndiv</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">nl</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">nf</span><span class="o">=</span><span class="mi">6</span><span class="p">)</span>
<span class="gh">Out[8]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727867460></span>
<span class="go"># Create a divergent style colormap with 4 divisions (two per side), each</span>
<span class="go"># spanning 5. with five filled intervals (so that the filled interval is 1) and</span>
<span class="go"># one contour line per division, omitting the zero contour line</span>
<span class="gp">In [9]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showvar</span><span class="p">(</span><span class="n">t2</span><span class="o">.</span><span class="n">U</span><span class="p">(</span><span class="n">pres</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">),</span> <span class="n">style</span><span class="o">=</span><span class="s1">'div'</span><span class="p">,</span> <span class="n">cdelt</span><span class="o">=</span><span class="mf">5.</span><span class="p">,</span> <span class="n">ndiv</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">nf</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">nozero</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gh">Out[9]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727734430></span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/cp_autoclfex1.png"><img alt="_images/cp_autoclfex1.png" src="_images/cp_autoclfex1.png" style="width: 4in;" /></a>
<a class="reference internal image-reference" href="_images/cp_autoclfex2.png"><img alt="_images/cp_autoclfex2.png" src="_images/cp_autoclfex2.png" style="width: 4in;" /></a>
<p>The interval algorithm is based on a larger “division”, which sets the tick
labels in the colorbar (and, along with the ‘style’, the colormap chosen by
default). Each division is split up into an equal number of filled intervals
(‘nf’) and lined intervals (‘nl’). The latter can be omitted entirely by
setting ‘nl’ to zero. The width of each interval can be specified explicitly
with ‘cdelt’; if this is not given an appropriate value will be guessed.</p>
<p>Other types of contouring can be produced by specifying the ‘type’
keyword argument; possible values include:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 18%" />
<col style="width: 52%" />
<col style="width: 30%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Value of ‘type’</p></th>
<th class="head"><p>Style of plot</p></th>
<th class="head"><p>Underlying helper function</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>‘clf’</p></td>
<td><p>Filled contour plot (default)</p></td>
<td><p><code class="xref py py-func docutils literal notranslate"><span class="pre">plot.clfdict()</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>‘cl’</p></td>
<td><p>Line contour plot</p></td>
<td><p><code class="xref py py-func docutils literal notranslate"><span class="pre">plot.cldict()</span></code></p></td>
</tr>
<tr class="row-even"><td><p>‘log’ or ‘log1s’</p></td>
<td><p>Logarithmically-spaced contour plot</p></td>
<td><p><code class="xref py py-func docutils literal notranslate"><span class="pre">plot.log1sdict()</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>‘log2s’</p></td>
<td><p>Two-sided, logarithmically-spaced contour plot</p></td>
<td><p><code class="xref py py-func docutils literal notranslate"><span class="pre">plot.log2sdict()</span></code></p></td>
</tr>
</tbody>
</table>
<p>How the rest of the arguments passed to <a class="reference internal" href="plot.html#pygeode.showvar" title="pygeode.showvar"><code class="xref py py-func docutils literal notranslate"><span class="pre">showvar()</span></code></a> are parsed depends on
this argument. In each case these make use of a particular helper function
which determines the appropriate contour intervals and colourmap; keyword
arguments are passed through to the underlying function. While these details
are transparent at this level, this is carried out by returning a dictionary of
keyword arguments which is then passed on to the underlying <a class="reference internal" href="plot.html#pygeode.vcontour" title="pygeode.vcontour"><code class="xref py py-func docutils literal notranslate"><span class="pre">vcontour()</span></code></a>
command which provides lower level control over the contouring performed.</p>
<div class="section" id="showlines-showgrid">
<h3>Showlines, Showgrid<a class="headerlink" href="#showlines-showgrid" title="Permalink to this headline">¶</a></h3>
<p>One often wants to plot multiple variables at once. Two useful commands for
this purpose are <a class="reference internal" href="plot.html#pygeode.showlines" title="pygeode.showlines"><code class="xref py py-func docutils literal notranslate"><span class="pre">showlines()</span></code></a> and <a class="reference internal" href="plot.html#pygeode.showgrid" title="pygeode.showgrid"><code class="xref py py-func docutils literal notranslate"><span class="pre">showgrid()</span></code></a>. Multiple
1-dimensional variables can be plotted on the same axes with
<a class="reference internal" href="plot.html#pygeode.showlines" title="pygeode.showlines"><code class="xref py py-func docutils literal notranslate"><span class="pre">showlines()</span></code></a>.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [10]: </span><span class="n">tm</span> <span class="o">=</span> <span class="n">t2</span><span class="o">.</span><span class="n">Temp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">,</span> <span class="s1">'lon'</span><span class="p">)</span>
<span class="go"># Multiple profiles</span>
<span class="go">#@savefig showlines_ex.png width=4in</span>
<span class="go">#In [2]: pyg.showlines([tm(lat=l) for l in [-60, -30, 0, 30, 60]])</span>
</pre></div>
</div>
<p>In a similar vein, multiple contour plots can be plotted in a grid with the
same contour intervals and a common colorbar.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="n">tm</span> <span class="o">=</span> <span class="n">t2</span><span class="o">.</span><span class="n">Temp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="s1">'time'</span><span class="p">)</span>
<span class="go"># Multiple contour plots</span>
<span class="gp">In [12]: </span><span class="n">pyg</span><span class="o">.</span><span class="n">showgrid</span><span class="p">([</span><span class="n">tm</span><span class="p">(</span><span class="n">pres</span><span class="o">=</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="mi">200</span><span class="p">,</span> <span class="mi">100</span><span class="p">]],</span> <span class="n">ncol</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">style</span><span class="o">=</span><span class="s1">'seq'</span><span class="p">,</span> <span class="nb">min</span><span class="o">=</span><span class="mi">180</span><span class="p">,</span> <span class="n">cdelt</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">ndiv</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="gh">Out[12]: </span><span class="go"><pygeode.plot.wrappers.AxesWrapper at 0x7f3727644670></span>
</pre></div>
</div>
<a class="reference internal image-reference" href="_images/showgrid_ex.png"><img alt="_images/showgrid_ex.png" src="_images/showgrid_ex.png" style="width: 8in;" /></a>
</div>
<div class="section" id="finer-control">
<h3>Finer control<a class="headerlink" href="#finer-control" title="Permalink to this headline">¶</a></h3>
<p>There are also lower-level plotting routines which can be used to construct
plots with a finer degree of control (see <a class="reference internal" href="plot.html"><span class="doc">Plot module</span></a> for a list). For examples
of how to use these routines, see the examples given in the <a class="reference internal" href="gallery/index.html"><span class="doc">Gallery</span></a>.</p>
</div>
</div>
</div>
</div>
<div class="bottomnav" role="navigation" aria-label="bottom navigation">
<p>
«  <a href="tut_io.html">Variable input and output</a>
  ::  
<a class="uplink" href="reference.html">Reference</a>
  ::  
<a class="uplink" href="tutorial.html">Tutorial</a>
  ::  
<a class="uplink" href="gallery/index.html">Gallery</a>
  ::  
<a href="tut_axes.html">Working with Axes</a>  »
</p>
</div>
<div class="footer" role="contentinfo">
© Copyright 2020, Mike Neish, Peter Hitchcock.
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2.
</div>
</body>
</html>