<spanid="sphx-glr-tutorials-images-py"></span><spanid="id1"></span><h1>Image tutorial<aclass="headerlink" href="#image-tutorial" title="Link to this heading">#</a></h1>
<p>A short tutorial on plotting images with Matplotlib.</p>
<sectionid="startup-commands">
<spanid="imaging-startup"></span><h2>Startup commands<aclass="headerlink" href="#startup-commands" title="Link to this heading">#</a></h2>
<p>First, let's start IPython. It is a most excellent enhancement to the
standard Python prompt, and it ties in especially well with
Matplotlib. Start IPython either directly at a shell, or with the Jupyter
Notebook (where IPython as a running kernel).</p>
<p>With IPython started, we now need to connect to a GUI event loop. This
tells IPython where (and how) to display plots. To connect to a GUI
loop, execute the <strong>%matplotlib</strong> magic at your IPython prompt. There's more
detail on exactly what this does at <aclass="reference external" href="https://ipython.readthedocs.io/en/stable/interactive/reference.html#gui-event-loop-support">IPython's documentation on GUI
event loops</a>.</p>
<p>If you're using Jupyter Notebook, the same commands are available, but
people commonly use a specific argument to the %matplotlib magic:</p>
<p>This turns on inline plotting, where plot graphics will appear in your
notebook. This has important implications for interactivity. For inline plotting, commands in
cells below the cell that outputs a plot will not affect the plot. For example,
changing the colormap is not possible from cells below the cell that creates a plot.
However, for other backends, such as Qt, that open a separate window,
cells below those that create the plot will change the plot - it is a
live object in memory.</p>
<p>This tutorial will use Matplotlib's implicit plotting interface, pyplot. This
interface maintains global state, and is very useful for quickly and easily
experimenting with various plot settings. The alternative is the explicit,
which is more suitable for large application development. For an explanation
of the tradeoffs between the implicit and explicit interfaces see
<aclass="reference internal" href="../users/explain/figure/api_interfaces.html#api-interfaces"><spanclass="std std-ref">Matplotlib Application Interfaces (APIs)</span></a> and the <aclass="reference internal" href="../users/explain/quick_start.html#quick-start"><spanclass="std std-ref">Quick start guide</span></a> to start using the explicit interface.
For now, let's get on with the implicit approach:</p>
<spanid="importing-data"></span><h2>Importing image data into Numpy arrays<aclass="headerlink" href="#importing-image-data-into-numpy-arrays" title="Link to this heading">#</a></h2>
<p>Matplotlib relies on the <aclass="reference external" href="https://pillow.readthedocs.io/en/latest/">Pillow</a> library to load image data.</p>
to your computer for the rest of this tutorial.</p>
<p>We use Pillow to open an image (with <aclass="reference external" href="https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open" title="(in Pillow (PIL Fork) v11.3.0)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">PIL.Image.open</span></code></a>), and immediately
convert the <aclass="reference external" href="https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image" title="(in Pillow (PIL Fork) v11.3.0)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">PIL.Image.Image</span></code></a> object into an 8-bit (<codeclass="docutils literal notranslate"><spanclass="pre">dtype=uint8</span></code>) numpy
<p>Each inner list represents a pixel. Here, with an RGB image, there
are 3 values. Since it's a black and white image, R, G, and B are all
similar. An RGBA (where A is alpha, or transparency) has 4 values
per inner list, and a simple luminance image just has one value (and
is thus only a 2-D array, not a 3-D array). For RGB and RGBA images,
Matplotlib supports float32 and uint8 data types. For grayscale,
Matplotlib supports only float32. If your array data does not meet
one of these descriptions, you need to rescale it.</p>
</section>
<sectionid="plotting-numpy-arrays-as-images">
<spanid="plotting-data"></span><h2>Plotting numpy arrays as images<aclass="headerlink" href="#plotting-numpy-arrays-as-images" title="Link to this heading">#</a></h2>
<p>So, you have your data in a numpy array (either by importing it, or by
generating it). Let's render it. In Matplotlib, this is performed
using the <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">imshow()</span></code></a> function. Here we'll grab
the plot object. This object gives you an easy way to manipulate the
<imgsrc="../_images/sphx_glr_images_001.png" srcset="../_images/sphx_glr_images_001.png, ../_images/sphx_glr_images_001_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>You can also plot any numpy array.</p>
<spanid="pseudocolor"></span><h3>Applying pseudocolor schemes to image plots<aclass="headerlink" href="#applying-pseudocolor-schemes-to-image-plots" title="Link to this heading">#</a></h3>
<p>Pseudocolor can be a useful tool for enhancing contrast and
visualizing your data more easily. This is especially useful when
making presentations of your data using projectors - their contrast is
typically quite poor.</p>
<p>Pseudocolor is only relevant to single-channel, grayscale, luminosity
images. We currently have an RGB image. Since R, G, and B are all
similar (see for yourself above or in your data), we can just pick one
channel of our data using array slicing (you can read more in the
<imgsrc="../_images/sphx_glr_images_002.png" srcset="../_images/sphx_glr_images_002.png, ../_images/sphx_glr_images_002_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>Now, with a luminosity (2D, no color) image, the default colormap (aka lookup table,
LUT), is applied. The default is called viridis. There are plenty of
<imgsrc="../_images/sphx_glr_images_003.png" srcset="../_images/sphx_glr_images_003.png, ../_images/sphx_glr_images_003_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>Note that you can also change colormaps on existing plot objects using the
<imgsrc="../_images/sphx_glr_images_004.png" srcset="../_images/sphx_glr_images_004.png, ../_images/sphx_glr_images_004_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>However, remember that in the Jupyter Notebook with the inline backend,
you can't make changes to plots that have already been rendered. If you
create imgplot here in one cell, you cannot call set_cmap() on it in a later
cell and expect the earlier plot to change. Make sure that you enter these
commands together in one cell. plt commands will not change plots from earlier
cells.</p>
</div>
<p>There are many other colormap schemes available. See the <aclass="reference internal" href="../users/explain/colors/colormaps.html#colormaps"><spanclass="std std-ref">list and images
of the colormaps</span></a>.</p>
</section>
<sectionid="color-scale-reference">
<spanid="color-bars"></span><h3>Color scale reference<aclass="headerlink" href="#color-scale-reference" title="Link to this heading">#</a></h3>
<p>It's helpful to have an idea of what value a color represents. We can
<imgsrc="../_images/sphx_glr_images_005.png" srcset="../_images/sphx_glr_images_005.png, ../_images/sphx_glr_images_005_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/></section>
<sectionid="examining-a-specific-data-range">
<spanid="data-ranges"></span><h3>Examining a specific data range<aclass="headerlink" href="#examining-a-specific-data-range" title="Link to this heading">#</a></h3>
<p>Sometimes you want to enhance the contrast in your image, or expand
the contrast in a particular region while sacrificing the detail in
colors that don't vary much, or don't matter. A good tool to find
interesting regions is the histogram. To create a histogram of our
image data, we use the <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.hist.html#matplotlib.pyplot.hist" title="matplotlib.pyplot.hist"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">hist()</span></code></a> function.</p>
<imgsrc="../_images/sphx_glr_images_006.png" srcset="../_images/sphx_glr_images_006.png, ../_images/sphx_glr_images_006_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>Most often, the "interesting" part of the image is around the peak,
and you can get extra contrast by clipping the regions above and/or
below the peak. In our histogram, it looks like there's not much
useful information in the high end (not many white things in the
image). Let's adjust the upper limit, so that we effectively "zoom in
on" part of the histogram. We do this by setting <em>clim</em>, the colormap
limits.</p>
<p>This can be done by passing a <em>clim</em> keyword argument in the call to
<imgsrc="../_images/sphx_glr_images_007.png" srcset="../_images/sphx_glr_images_007.png, ../_images/sphx_glr_images_007_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>This can also be done by calling the
<aclass="reference internal" href="../api/cm_api.html#matplotlib.cm.ScalarMappable.set_clim" title="matplotlib.cm.ScalarMappable.set_clim"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_clim()</span></code></a> method of the returned image
plot object, but make sure that you do so in the same cell as your plot
command when working with the Jupyter Notebook - it will not change
<imgsrc="../_images/sphx_glr_images_008.png" srcset="../_images/sphx_glr_images_008.png, ../_images/sphx_glr_images_008_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/></section>
<sectionid="array-interpolation-schemes">
<spanid="interpolation"></span><h3>Array Interpolation schemes<aclass="headerlink" href="#array-interpolation-schemes" title="Link to this heading">#</a></h3>
<p>Interpolation calculates what the color or value of a pixel "should"
be, according to different mathematical schemes. One common place
that this happens is when you resize an image. The number of pixels
change, but you want the same information. Since pixels are discrete,
there's missing space. Interpolation is how you fill that space.
This is why your images sometimes come out looking pixelated when you
blow them up. The effect is more pronounced when the difference
between the original image and the expanded image is greater. Let's
take our image and shrink it. We're effectively discarding pixels,
only keeping a select few. Now when we plot it, that data gets blown
up to the size on your screen. The old pixels aren't there anymore,
and the computer has to draw in pixels to fill that space.</p>
<p>We'll use the Pillow library that we used to load the image also to resize
<imgsrc="../_images/sphx_glr_images_009.png" srcset="../_images/sphx_glr_images_009.png, ../_images/sphx_glr_images_009_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>Here we use the default interpolation ("nearest"), since we did not
give <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.imshow.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">imshow()</span></code></a> any interpolation argument.</p>
<p>Let's try some others. Here's "bilinear":</p>
<imgsrc="../_images/sphx_glr_images_011.png" srcset="../_images/sphx_glr_images_011.png, ../_images/sphx_glr_images_011_2_00x.png 2.00x" alt="images" class = "sphx-glr-single-img"/><p>Bicubic interpolation is often used when blowing up photos - people
tend to prefer blurry over pixelated.</p>
<pclass="sphx-glr-timing"><strong>Total running time of the script:</strong> (0 minutes 8.451 seconds)</p>
<liclass="toc-h3 nav-item toc-entry"><aclass="reference internal nav-link" href="#examining-a-specific-data-range">Examining a specific data range</a></li>