|
28 | 28 | # ProPlot works by creating a `proplot.figure.Figure` subclass of the |
29 | 29 | # matplotlib figure class `~matplotlib.figure.Figure`, and a `proplot.axes.Axes` |
30 | 30 | # subclass of the matplotlib axes class `~matplotlib.axes.Axes`. |
31 | | -# All plotting in ProPlot begins by generating |
32 | | -# an instance of the new figure class filled with instances of the new |
33 | | -# axes classes using the `~proplot.ui.subplots` command, which is modeled |
34 | | -# after `matplotlib.pyplot.subplots`. |
35 | | -# ProPlot's `~proplot.ui.subplots` command can be used as follows: |
36 | 31 | # |
37 | | -# * Without any arguments, `~proplot.ui.subplots` returns a figure with a |
| 32 | +# The `~proplot.ui.subplots` command is used to create ProPlot figures. Modeled after |
| 33 | +# `matplotlib.pyplot.subplots`, it generates a `proplot.figure.Figure` instance filled |
| 34 | +# with `proplot.axes.Axes` instances. `~proplot.ui.subplots` can be used as follows: |
| 35 | +# |
| 36 | +# * With no arguments, `~proplot.ui.subplots` returns a figure with a |
38 | 37 | # single subplot. |
39 | 38 | # * With `ncols` or `nrows`, `~proplot.ui.subplots` returns a |
40 | 39 | # figure with a simple grid of subplots. |
|
44 | 43 | # `~matplotlib.gridspec.GridSpec` slot that is occupied by the corresponding |
45 | 44 | # subplot and ``0`` indicates an empty space. |
46 | 45 | # |
47 | | -# In the below examples, we create subplot grids with `~proplot.ui.subplots` |
48 | | -# and modify the axes labels. See the :ref:`formatting guide <ug_format>` |
49 | | -# and :ref:`subplots container <ug_container>` sections for details. |
| 46 | +# In the below examples, we create a few simple figures with `~proplot.ui.subplots`. |
| 47 | +# See the next sections for details. |
50 | 48 |
|
51 | 49 | # %% [raw] raw_mimetype="text/restructuredtext" |
52 | 50 | # .. note:: |
|
55 | 53 | # `matplotlib backend <https://matplotlib.org/faq/usage_faq#what-is-a-backend>`__ |
56 | 54 | # -- the default background color is white when the figure is saved. This is done |
57 | 55 | # by setting :rcraw:`figure.facecolor` to gray, in order to improve contrast |
58 | | -# when working with figures, similar to MATLAB. ProPlot also changes the default |
| 56 | +# when working with figures, similar to MATLAB. |
| 57 | +# |
| 58 | +# ProPlot also changes the default |
59 | 59 | # :rcraw:`savefig.format` from PNG to PDF for the following reasons: |
60 | 60 | # |
61 | 61 | # #. Vector graphic formats are infinitely scalable. |
|
132 | 132 | # %% [raw] raw_mimetype="text/restructuredtext" |
133 | 133 | # .. _ug_plots: |
134 | 134 | # |
135 | | -# Plotting data |
136 | | -# ------------- |
| 135 | +# Plotting stuff |
| 136 | +# -------------- |
137 | 137 | # |
138 | 138 | # Matplotlib has |
139 | | -# `two different APIs <https://matplotlib.org/3.2.1/api/index.html>`__: |
140 | | -# an object-oriented API and a MATLAB-style |
141 | | -# `~matplotlib.pyplot` API (which uses the object-oriented API internally). |
142 | | -# Plotting in ProPlot is just like plotting in matplotlib with |
143 | | -# the *object-oriented* API. Rather than creating |
144 | | -# a brand new interface, ProPlot simply builds upon the existing matplotlib |
145 | | -# constructs of the `~matplotlib.axes.Axes` and the `~matplotlib.figure.Figure` |
146 | | -# by adding new commands and new options to existing commands, without changing |
147 | | -# the usage or syntax. This means a shallow learning curve for the average |
148 | | -# matplotlib user. |
| 139 | +# `two different interfaces <https://matplotlib.org/3.2.1/api/index.html>`__: |
| 140 | +# an object-oriented interface and a MATLAB-style `~matplotlib.pyplot` interface |
| 141 | +# (which uses the object-oriented interface internally). Plotting with ProPlot is |
| 142 | +# just like plotting with matplotlib's *object-oriented* interface. Proplot builds |
| 143 | +# upon the matplotlib constructs of the `~matplotlib.figure.Figure` and the |
| 144 | +# `~matplotlib.axes.Axes` by adding new commands and adding new features to |
| 145 | +# existing commands. These additions do not change the usage or syntax of existing |
| 146 | +# commands, which means a shallow learning curve for the average matplotlib user. |
149 | 147 | # |
150 | 148 | # In the below example, we create a 4-panel figure with the familiar matplotlib |
151 | 149 | # commands `~matplotlib.axes.Axes.plot`, `~matplotlib.axes.Axes.scatter`, |
152 | 150 | # `~matplotlib.axes.Axes.pcolormesh`, and `~matplotlib.axes.Axes.contourf`. |
153 | 151 | # See the :ref:`1d plotting <ug_1dplots>` and :ref:`2d plotting <ug_2dplots>` |
154 | | -# sections for details on the plotting features added by ProPlot. |
| 152 | +# sections for details on the features added by ProPlot. |
155 | 153 |
|
156 | 154 |
|
157 | 155 | # %% |
|
176 | 174 | # %% [raw] raw_mimetype="text/restructuredtext" |
177 | 175 | # .. _ug_format: |
178 | 176 | # |
179 | | -# Formatting plots |
| 177 | +# Formatting stuff |
180 | 178 | # ---------------- |
181 | 179 | # |
182 | 180 | # Every `~matplotlib.axes.Axes` returned by `~proplot.ui.subplots` has a |
|
191 | 189 | # `proplot.axes.CartesianAxes.format`, `proplot.axes.PolarAxes.format`, or |
192 | 190 | # `proplot.axes.GeoAxes.format`. These change settings that are |
193 | 191 | # specific to the axes type. For example: |
194 | | - |
| 192 | +# |
195 | 193 | # * To change the *x* axis bounds on a `~proplot.axes.CartesianAxes`, |
196 | 194 | # use e.g. ``xlim=(0, 5)``. |
197 | 195 | # * To change the radial bounds on a `~proplot.axes.PolarAxes`, use e.g. |
|
242 | 240 | xtickdir='inout', xtickminor=False, ygridminor=True, |
243 | 241 | ) |
244 | 242 |
|
| 243 | +# %% [raw] raw_mimetype="text/restructuredtext" |
| 244 | +# .. _ug_container: |
| 245 | +# |
| 246 | +# Formatting all-at-once |
| 247 | +# ---------------------- |
| 248 | +# |
| 249 | +# Instead of an `~numpy.ndarray` of subplots, `~proplot.ui.subplots` returns a |
| 250 | +# `~proplot.ui.SubplotsContainer`. This container behaves like an |
| 251 | +# `~matplotlib.axes.Axes` object when it contains just one axes, and behaves |
| 252 | +# like a list otherwise. It supports both 1D indexing (e.g. ``axs[1]``) and |
| 253 | +# 2D indexing (e.g. ``axs[0, 1]``), and is row-major by default. Slicing a |
| 254 | +# `~proplot.ui.SubplotsContainer` returns another container (e.g. ``axs[:, 0]``). |
| 255 | +# |
| 256 | +# `~proplot.ui.SubplotsContainer` is useful because it lets you call |
| 257 | +# `~proplot.axes.Axes` methods simultaneously for all subplots in the container. |
| 258 | +# In the below example, we use the `~proplot.ui.SubplotsContainer` returned by |
| 259 | +# `~proplot.ui.subplots` with the `proplot.axes.Axes.format` command to format |
| 260 | +# several subplots at once. |
| 261 | + |
| 262 | +# %% |
| 263 | +import proplot as plot |
| 264 | +import numpy as np |
| 265 | +state = np.random.RandomState(51423) |
| 266 | +fig, axs = plot.subplots(ncols=4, nrows=4, axwidth=1.2) |
| 267 | +axs.format( |
| 268 | + xlabel='xlabel', ylabel='ylabel', suptitle='SubplotsContainer demo', |
| 269 | + grid=False, xlim=(0, 50), ylim=(-4, 4) |
| 270 | +) |
| 271 | + |
| 272 | +# Various ways to select subplots in the container |
| 273 | +axs[:, 0].format(facecolor='blush', color='gray7', linewidth=1) |
| 274 | +axs[0, :].format(facecolor='sky blue', color='gray7', linewidth=1) |
| 275 | +axs[0].format(color='black', facecolor='gray5', linewidth=1.4) |
| 276 | +axs[1:, 1:].format(facecolor='gray1') |
| 277 | +for ax in axs[1:, 1:]: |
| 278 | + ax.plot((state.rand(50, 5) - 0.5).cumsum(axis=0), cycle='Grays', lw=2) |
| 279 | + |
245 | 280 |
|
246 | 281 | # %% [raw] raw_mimetype="text/restructuredtext" |
247 | 282 | # .. _ug_rc: |
248 | 283 | # |
249 | | -# Changing rc settings |
250 | | -# -------------------- |
| 284 | +# Styles and settings |
| 285 | +# ------------------- |
251 | 286 | # |
252 | 287 | # A special object named `~proplot.config.rc` is created whenever you import |
253 | 288 | # ProPlot. `~proplot.config.rc` is similar to the matplotlib |
|
326 | 361 | for ax, style in zip(axs, styles): |
327 | 362 | ax.format(style=style, xlabel='xlabel', ylabel='ylabel', title=style) |
328 | 363 | ax.plot(data, linewidth=3) |
329 | | - |
330 | | - |
331 | | -# %% [raw] raw_mimetype="text/restructuredtext" |
332 | | -# .. _ug_container: |
333 | | -# |
334 | | -# Subplots containers |
335 | | -# ------------------- |
336 | | -# |
337 | | -# Instead of an `~numpy.ndarray` of axes, `~proplot.ui.subplots` returns a |
338 | | -# `~proplot.ui.SubplotsContainer` instance. This container behaves like an |
339 | | -# `~matplotlib.axes.Axes` object when it contains just one axes, and behaves |
340 | | -# like a list otherwise. It supports both 1D indexing (e.g. ``axs[1]``) and |
341 | | -# 2D indexing (e.g. ``axs[0, 1]``), and is row-major by default. Slicing a |
342 | | -# `~proplot.ui.SubplotsContainer` returns another container (e.g. ``axs[:, 0]``), |
343 | | -# and `~proplot.axes.Axes` methods can be called simultaneously for all axes in the |
344 | | -# container by calling the method from the container (e.g. ``axs.format(abc=True)``). |
345 | | -# |
346 | | -# In the below example, the `~proplot.ui.SubplotsContainer` returned by |
347 | | -# `~proplot.ui.subplots` is used to cusomtize several axes at once with |
348 | | -# `proplot.axes.Axes.format`. |
349 | | - |
350 | | -# %% |
351 | | -import proplot as plot |
352 | | -import numpy as np |
353 | | -state = np.random.RandomState(51423) |
354 | | -fig, axs = plot.subplots(ncols=4, nrows=4, axwidth=1.2) |
355 | | -axs.format( |
356 | | - xlabel='xlabel', ylabel='ylabel', suptitle='SubplotsContainer demo', |
357 | | - grid=False, xlim=(0, 50), ylim=(-4, 4) |
358 | | -) |
359 | | - |
360 | | -# Various ways to select subplots in the container |
361 | | -axs[:, 0].format(facecolor='blush', color='gray7', linewidth=1) |
362 | | -axs[0, :].format(facecolor='sky blue', color='gray7', linewidth=1) |
363 | | -axs[0].format(color='black', facecolor='gray5', linewidth=1.4) |
364 | | -axs[1:, 1:].format(facecolor='gray1') |
365 | | -for ax in axs[1:, 1:]: |
366 | | - ax.plot((state.rand(50, 5) - 0.5).cumsum(axis=0), cycle='Grays', lw=2) |
0 commit comments