<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>pylab_examples example code: image_interp.py — Matplotlib 1.2.0 documentation</title> <link rel="stylesheet" href="../../_static/mpl.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.2.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Matplotlib 1.2.0 documentation" href="../../_static/opensearch.xml"/> <link rel="top" title="Matplotlib 1.2.0 documentation" href="../../index.html" /> </head> <body> <!-- Piwik --> <script type="text/javascript"> if ("matplotlib.sourceforge.net" == document.location.hostname || "matplotlib.sf.net" == document.location.hostname) { var pkBaseURL = (("https:" == document.location.protocol) ? "https://apps.sourceforge.net/piwik/matplotlib/" : "http://apps.sourceforge.net/piwik/matplotlib/"); document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E")); } </script> <script type="text/javascript"> if ("matplotlib.sourceforge.net" == document.location.hostname || "matplotlib.sf.net" == document.location.hostname) { piwik_action_name = ''; piwik_idsite = 1; piwik_url = pkBaseURL + "piwik.php"; piwik_log(piwik_action_name, piwik_idsite, piwik_url); document.write(unescape('%3Cobject%3E%3Cnoscript%3E%3Cp%3E%3Cimg src="http://apps.sourceforge.net/piwik/matplotlib/piwik.php?idsite=1" alt="piwik"/%3E%3C/p%3E%3C/noscript%3E%3C/object%3E')); } </script> <!-- End Piwik Tag --> <link rel="shortcut icon" href="_static/favicon.ico"> <div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px"> <a href="../../index.html"><img src="../../_static/logo2.png" border="0" alt="matplotlib"/></a> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li><a href="../../index.html">home</a>| </li> <li><a href="../../search.html">search</a>| </li> <li><a href="../index.html">examples</a>| </li> <li><a href="../../gallery.html">gallery</a>| </li> <li><a href="../../contents.html">docs</a> »</li> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/examples/pylab_examples/image_interp.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="pylab-examples-example-code-image-interp-py"> <span id="pylab-examples-image-interp"></span><h1>pylab_examples example code: image_interp.py<a class="headerlink" href="#pylab-examples-example-code-image-interp-py" title="Permalink to this headline">ΒΆ</a></h1> <p>(<a class="reference external" href="../../mpl_examples/pylab_examples/image_interp.py">Source code</a>)</p> <div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/env python</span> <span class="sd">"""</span> <span class="sd">The same (small) array, interpolated with three different</span> <span class="sd">interpolation methods.</span> <span class="sd">The center of the pixel at A[i,j] is plotted at i+0.5, i+0.5. If you</span> <span class="sd">are using interpolation='nearest', the region bounded by (i,j) and</span> <span class="sd">(i+1,j+1) will have the same color. If you are using interpolation,</span> <span class="sd">the pixel center will have the same color as it does with nearest, but</span> <span class="sd">other pixels will be interpolated between the neighboring pixels.</span> <span class="sd">Earlier versions of matplotlib (<0.63) tried to hide the edge effects</span> <span class="sd">from you by setting the view limits so that they would not be visible.</span> <span class="sd">A recent bugfix in antigrain, and a new implementation in the</span> <span class="sd">matplotlib._image module which takes advantage of this fix, no longer</span> <span class="sd">makes this necessary. To prevent edge effects, when doing</span> <span class="sd">interpolation, the matplotlib._image module now pads the input array</span> <span class="sd">with identical pixels around the edge. Eg, if you have a 5x5 array</span> <span class="sd">with colors a-y as below</span> <span class="sd"> a b c d e</span> <span class="sd"> f g h i j</span> <span class="sd"> k l m n o</span> <span class="sd"> p q r s t</span> <span class="sd"> u v w x y</span> <span class="sd">the _image module creates the padded array,</span> <span class="sd"> a a b c d e e</span> <span class="sd"> a a b c d e e</span> <span class="sd"> f f g h i j j</span> <span class="sd"> k k l m n o o</span> <span class="sd"> p p q r s t t</span> <span class="sd"> o u v w x y y</span> <span class="sd"> o u v w x y y</span> <span class="sd">does the interpolation/resizing, and then extracts the central region.</span> <span class="sd">This allows you to plot the full range of your array w/o edge effects,</span> <span class="sd">and for example to layer multiple images of different sizes over one</span> <span class="sd">another with different interpolation methods - see</span> <span class="sd">examples/layer_images.py. It also implies a performance hit, as this</span> <span class="sd">new temporary, padded array must be created. Sophisticated</span> <span class="sd">interpolation also implies a performance hit, so if you need maximal</span> <span class="sd">performance or have very large images, interpolation='nearest' is</span> <span class="sd">suggested.</span> <span class="sd">"""</span> <span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span> <span class="n">A</span> <span class="o">=</span> <span class="n">rand</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span> <span class="n">figure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">imshow</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="s">'nearest'</span><span class="p">)</span> <span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="n">figure</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="n">imshow</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="s">'bilinear'</span><span class="p">)</span> <span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="n">figure</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="n">imshow</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">interpolation</span><span class="o">=</span><span class="s">'bicubic'</span><span class="p">)</span> <span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>Keywords: python, matplotlib, pylab, example, codex (see <a class="reference internal" href="../../faq/howto_faq.html#how-to-search-examples"><em>Search examples</em></a>)</p> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li><a href="../../index.html">home</a>| </li> <li><a href="../../search.html">search</a>| </li> <li><a href="../index.html">examples</a>| </li> <li><a href="../../gallery.html">gallery</a>| </li> <li><a href="../../contents.html">docs</a> »</li> </ul> </div> <div class="footer"> © Copyright 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team. Last updated on Jul 23, 2013. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>