<!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: pythonic_matplotlib.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/pythonic_matplotlib.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-pythonic-matplotlib-py"> <span id="pylab-examples-pythonic-matplotlib"></span><h1>pylab_examples example code: pythonic_matplotlib.py<a class="headerlink" href="#pylab-examples-example-code-pythonic-matplotlib-py" title="Permalink to this headline">ΒΆ</a></h1> <p>(<a class="reference external" href="../../mpl_examples/pylab_examples/pythonic_matplotlib.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">Some people prefer to write more pythonic, object oriented, code</span> <span class="sd">rather than use the pylab interface to matplotlib. This example shows</span> <span class="sd">you how.</span> <span class="sd">Unless you are an application developer, I recommend using part of the</span> <span class="sd">pylab interface, particularly the figure, close, subplot, axes, and</span> <span class="sd">show commands. These hide a lot of complexity from you that you don't</span> <span class="sd">need to see in normal figure creation, like instantiating DPI</span> <span class="sd">instances, managing the bounding boxes of the figure elements,</span> <span class="sd">creating and reaslizing GUI windows and embedding figures in them.</span> <span class="sd">If you are an application developer and want to embed matplotlib in</span> <span class="sd">your application, follow the lead of examples/embedding_in_wx.py,</span> <span class="sd">examples/embedding_in_gtk.py or examples/embedding_in_tk.py. In this</span> <span class="sd">case you will want to control the creation of all your figures,</span> <span class="sd">embedding them in application windows, etc.</span> <span class="sd">If you are a web application developer, you may want to use the</span> <span class="sd">example in webapp_demo.py, which shows how to use the backend agg</span> <span class="sd">figure canvase directly, with none of the globals (current figure,</span> <span class="sd">current axes) that are present in the pylab interface. Note that</span> <span class="sd">there is no reason why the pylab interface won't work for web</span> <span class="sd">application developers, however.</span> <span class="sd">If you see an example in the examples dir written in pylab interface,</span> <span class="sd">and you want to emulate that using the true python method calls, there</span> <span class="sd">is an easy mapping. Many of those examples use 'set' to control</span> <span class="sd">figure properties. Here's how to map those commands onto instance</span> <span class="sd">methods</span> <span class="sd">The syntax of set is</span> <span class="sd"> setp(object or sequence, somestring, attribute)</span> <span class="sd">if called with an object, set calls</span> <span class="sd"> object.set_somestring(attribute)</span> <span class="sd">if called with a sequence, set does</span> <span class="sd"> for object in sequence:</span> <span class="sd"> object.set_somestring(attribute)</span> <span class="sd">So for your example, if a is your axes object, you can do</span> <span class="sd"> a.set_xticklabels([])</span> <span class="sd"> a.set_yticklabels([])</span> <span class="sd"> a.set_xticks([])</span> <span class="sd"> a.set_yticks([])</span> <span class="sd">"""</span> <span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="n">figure</span><span class="p">,</span> <span class="n">show</span> <span class="kn">from</span> <span class="nn">numpy</span> <span class="kn">import</span> <span class="n">arange</span><span class="p">,</span> <span class="n">sin</span><span class="p">,</span> <span class="n">pi</span> <span class="n">t</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span> <span class="n">fig</span> <span class="o">=</span> <span class="n">figure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">ax1</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="o">*</span><span class="n">t</span><span class="p">))</span> <span class="n">ax1</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span> <span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_ylabel</span><span class="p">(</span><span class="s">'1 Hz'</span><span class="p">)</span> <span class="n">ax1</span><span class="o">.</span><span class="n">set_title</span><span class="p">(</span><span class="s">'A sine wave or two'</span><span class="p">)</span> <span class="k">for</span> <span class="n">label</span> <span class="ow">in</span> <span class="n">ax1</span><span class="o">.</span><span class="n">get_xticklabels</span><span class="p">():</span> <span class="n">label</span><span class="o">.</span><span class="n">set_color</span><span class="p">(</span><span class="s">'r'</span><span class="p">)</span> <span class="n">ax2</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">)</span> <span class="n">ax2</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">sin</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="o">*</span><span class="n">pi</span><span class="o">*</span><span class="n">t</span><span class="p">))</span> <span class="n">ax2</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span> <span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span> <span class="p">)</span> <span class="n">l</span> <span class="o">=</span> <span class="n">ax2</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s">'Hi mom'</span><span class="p">)</span> <span class="n">l</span><span class="o">.</span><span class="n">set_color</span><span class="p">(</span><span class="s">'g'</span><span class="p">)</span> <span class="n">l</span><span class="o">.</span><span class="n">set_fontsize</span><span class="p">(</span><span class="s">'large'</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>