<divid="unreleased-message"> You are reading an old version of the documentation (v1.5.3). For the latest version see <ahref="/stable/">https://matplotlib.org/stable/</a></div>
<spanid="id1"></span><h1>Image tutorial<aclass="headerlink" href="#image-tutorial" title="Permalink to this headline">¶</a></h1>
<divclass="section" id="startup-commands">
<spanid="imaging-startup"></span><h2>Startup commands<aclass="headerlink" href="#startup-commands" title="Permalink to this headline">¶</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 at a shell, or the IPython Notebook now.</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="http://ipython.org/ipython-doc/2/interactive/reference.html#gui-event-loop-support">IPython’s documentation on GUI
event loops</a>.</p>
<p>If you’re using IPython 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 color map is not possible from cells below the cell that creates a plot.
However, for other backends, such as qt4, 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 imperative-style 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 object-oriented interface, which is also
very powerful, and generally more suitable for large application
development. If you’d like to learn about the object-oriented
interface, a great place to start is our <aclass="reference external" href="http://matplotlib.org/faq/usage_faq.html">FAQ on usage</a>. For now, let’s get on
<spanid="importing-data"></span><h2>Importing image data into Numpy arrays<aclass="headerlink" href="#importing-image-data-into-numpy-arrays" title="Permalink to this headline">¶</a></h2>
<p>Loading image data is supported by the <aclass="reference external" href="http://python-imaging.github.io/">Pillow</a> library. Natively, matplotlib only
supports PNG images. The commands shown below fall back on Pillow if the
native read fails.</p>
<p>The image used in this example is a PNG file, but keep that Pillow
requirement in mind for your own data.</p>
<p>Here’s the image we’re going to play with:</p>
<spanid="plotting-data"></span><h2>Plotting numpy arrays as images<aclass="headerlink" href="#plotting-numpy-arrays-as-images" title="Permalink to this headline">¶</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/pyplot_api.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><codeclass="xref py py-func docutils literal"><spanclass="pre">imshow()</span></code></a> function. Here we’ll grab
the plot object. This object gives you an easy way to manipulate the
<spanid="pseudocolor"></span><h3>Applying pseudocolor schemes to image plots<aclass="headerlink" href="#applying-pseudocolor-schemes-to-image-plots" title="Permalink to this headline">¶</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
<p>This is array slicing. You can read more in the <aclass="reference external" href="http://www.scipy.org/Tentative_NumPy_Tutorial">Numpy tutorial</a>.</p>
<spanid="data-ranges"></span><h3>Examining a specific data range<aclass="headerlink" href="#examining-a-specific-data-range" title="Permalink to this headline">¶</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/pyplot_api.html#matplotlib.pyplot.hist" title="matplotlib.pyplot.hist"><codeclass="xref py py-func docutils literal"><spanclass="pre">hist()</span></code></a> function.</p>
<spanid="interpolation"></span><h3>Array Interpolation schemes<aclass="headerlink" href="#array-interpolation-schemes" title="Permalink to this headline">¶</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