|
7 | 7 | Image tutorial |
8 | 8 | ============== |
9 | 9 |
|
10 | | -A short tutorial on plotting images with Matplotlib. |
11 | | -
|
12 | | -.. _imaging_startup: |
13 | | -
|
14 | | -Startup commands |
15 | | -=================== |
16 | | -
|
17 | | -First, let's start IPython. It is a most excellent enhancement to the |
18 | | -standard Python prompt, and it ties in especially well with |
19 | | -Matplotlib. Start IPython either directly at a shell, or with the Jupyter |
20 | | -Notebook (where IPython as a running kernel). |
21 | | -
|
22 | | -With IPython started, we now need to connect to a GUI event loop. This |
23 | | -tells IPython where (and how) to display plots. To connect to a GUI |
24 | | -loop, execute the **%matplotlib** magic at your IPython prompt. There's more |
25 | | -detail on exactly what this does at `IPython's documentation on GUI |
26 | | -event loops |
27 | | -<https://ipython.readthedocs.io/en/stable/interactive/reference.html#gui-event-loop-support>`_. |
28 | | -
|
29 | | -If you're using Jupyter Notebook, the same commands are available, but |
30 | | -people commonly use a specific argument to the %matplotlib magic: |
31 | | -
|
32 | | -.. sourcecode:: ipython |
33 | | -
|
34 | | - In [1]: %matplotlib inline |
35 | | -
|
36 | | -This turns on inline plotting, where plot graphics will appear in your notebook. This |
37 | | -has important implications for interactivity. For inline plotting, commands in |
38 | | -cells below the cell that outputs a plot will not affect the plot. For example, |
39 | | -changing the colormap is not possible from cells below the cell that creates a plot. |
40 | | -However, for other backends, such as Qt, that open a separate window, |
41 | | -cells below those that create the plot will change the plot - it is a |
42 | | -live object in memory. |
43 | | -
|
44 | 10 | This tutorial will use Matplotlib's implicit plotting interface, pyplot. This |
45 | 11 | interface maintains global state, and is very useful for quickly and easily |
46 | 12 | experimenting with various plot settings. The alternative is the explicit, |
|
147 | 113 |
|
148 | 114 | # %% |
149 | 115 | # |
150 | | -# .. note:: |
151 | | -# |
152 | | -# However, remember that in the Jupyter Notebook with the inline backend, |
153 | | -# you can't make changes to plots that have already been rendered. If you |
154 | | -# create imgplot here in one cell, you cannot call set_cmap() on it in a later |
155 | | -# cell and expect the earlier plot to change. Make sure that you enter these |
156 | | -# commands together in one cell. plt commands will not change plots from earlier |
157 | | -# cells. |
158 | | -# |
159 | 116 | # There are many other colormap schemes available. See the :ref:`list and images |
160 | 117 | # of the colormaps<colormaps>`. |
161 | 118 | # |
|
201 | 158 | # %% |
202 | 159 | # This can also be done by calling the |
203 | 160 | # :meth:`~matplotlib.cm.ScalarMappable.set_clim` method of the returned image |
204 | | -# plot object, but make sure that you do so in the same cell as your plot |
205 | | -# command when working with the Jupyter Notebook - it will not change |
206 | | -# plots from earlier cells. |
| 161 | +# plot object. |
207 | 162 |
|
208 | 163 | imgplot = plt.imshow(lum_img) |
209 | 164 | imgplot.set_clim(0, 175) |
|
0 commit comments