<divid="unreleased-message"> You are reading an old version of the documentation (v1.2.1). 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>At the very least, you’ll need to have access to the
<aclass="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.imshow" title="matplotlib.pyplot.imshow"><ttclass="xref py py-func docutils literal"><spanclass="pre">imshow()</span></tt></a> function. There are a couple of
ways to do it. The easy way for an interactive environment:</p>
<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>Plotting image data is supported by the Python Image Library (<aclass="reference external" href="http://www.pythonware.com/products/pil/">PIL</a>). Natively, matplotlib
only supports PNG images. The commands shown below fall back on PIL
if the native read fails.</p>
<p>The image used in this example is a PNG file, but keep that PIL
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"><ttclass="xref py py-func docutils literal"><spanclass="pre">imshow()</span></tt></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>
<p>There are many other colormap schemes available. See the <aclass="reference external" href="http://matplotlib.org/api/pyplot_summary.html#matplotlib.pyplot.colormaps">list of
colormaps</a>
and <aclass="reference external" href="http://matplotlib.org/examples/pylab_examples/show_colormaps.html">images of the colormaps</a>.</p>
</div>
<divclass="section" id="color-scale-reference">
<spanid="color-bars"></span><h3>Color scale reference<aclass="headerlink" href="#color-scale-reference" title="Permalink to this headline">¶</a></h3>
<p>It’s helpful to have an idea of what value a color represents. We can
do that by adding color bars. It’s as easy as one line:</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"><ttclass="xref py py-func docutils literal"><spanclass="pre">hist()</span></tt></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>
<spanclass="gp">In [9]: </span><spanclass="n">img</span><spanclass="o">=</span><spanclass="n">Image</span><spanclass="o">.</span><spanclass="n">open</span><spanclass="p">(</span><spanclass="s">'stinkbug.png'</span><spanclass="p">)</span><spanclass="c"># Open image as PIL image object</span>
<spanclass="gp">In [10]: </span><spanclass="n">rsize</span><spanclass="o">=</span><spanclass="n">img</span><spanclass="o">.</span><spanclass="n">resize</span><spanclass="p">((</span><spanclass="n">img</span><spanclass="o">.</span><spanclass="n">size</span><spanclass="p">[</span><spanclass="mi">0</span><spanclass="p">]</span><spanclass="o">/</span><spanclass="mi">10</span><spanclass="p">,</span><spanclass="n">img</span><spanclass="o">.</span><spanclass="n">size</span><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">]</span><spanclass="o">/</span><spanclass="mi">10</span><spanclass="p">))</span><spanclass="c"># Use PIL to resize</span>
<spanclass="gp">In [11]: </span><spanclass="n">rsizeArr</span><spanclass="o">=</span><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">asarray</span><spanclass="p">(</span><spanclass="n">rsize</span><spanclass="p">)</span><spanclass="c"># Get array back</span>