<divid="unreleased-message"> You are reading an old version of the documentation (v1.3.1). For the latest version see <ahref="/stable/">https://matplotlib.org/stable/</a></div>
<spanid="mpl-shell"></span><h1>Using matplotlib in a python shell<aclass="headerlink" href="#using-matplotlib-in-a-python-shell" title="Permalink to this headline">¶</a></h1>
<p>By default, matplotlib defers drawing until the end of the script
because drawing can be an expensive operation, and you may not want
to update the plot every time a single property is changed, only once
after all the properties have changed.</p>
<p>But when working from the python shell, you usually do want to update
the plot with every command, eg, after changing the
<aclass="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.xlabel" title="matplotlib.pyplot.xlabel"><ttclass="xref py py-func docutils literal"><spanclass="pre">xlabel()</span></tt></a>, or the marker style of a line.
While this is simple in concept, in practice it can be tricky, because
matplotlib is a graphical user interface application under the hood,
and there are some tricks to make the applications work right in a
python shell.</p>
<divclass="section" id="ipython-to-the-rescue">
<spanid="ipython-pylab"></span><h2>Ipython to the rescue<aclass="headerlink" href="#ipython-to-the-rescue" title="Permalink to this headline">¶</a></h2>
<p>Fortunately, <aclass="reference external" href="http://ipython.org/">ipython</a>, an enhanced
interactive python shell, has figured out all of these tricks, and is
matplotlib aware, so when you start ipython in the <em>pylab</em> mode.</p>
<p>it sets everything up for you so interactive plotting works as you
would expect it to. Call <aclass="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.figure" title="matplotlib.pyplot.figure"><ttclass="xref py py-func docutils literal"><spanclass="pre">figure()</span></tt></a> and a
figure window pops up, call <aclass="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.plot" title="matplotlib.pyplot.plot"><ttclass="xref py py-func docutils literal"><spanclass="pre">plot()</span></tt></a> and your
data appears in the figure window.</p>
<p>Note in the example above that we did not import any matplotlib names
because in pylab mode, ipython will import them automatically.
ipython also turns on <em>interactive</em> mode for you, which causes every
pyplot command to trigger a figure update, and also provides a
matplotlib aware <ttclass="docutils literal"><spanclass="pre">run</span></tt> command to run matplotlib scripts
efficiently. ipython will turn off interactive mode during a <ttclass="docutils literal"><spanclass="pre">run</span></tt>
command, and then restore the interactive state at the end of the
run so you can continue tweaking the figure manually.</p>
<p>There has been a lot of recent work to embed ipython, with pylab
support, into various GUI applications, so check on the ipython
mailing <aclass="reference external" href="http://projects.scipy.org/mailman/listinfo/ipython-user">list</a> for the
<spanid="other-shells"></span><h2>Other python interpreters<aclass="headerlink" href="#other-python-interpreters" title="Permalink to this headline">¶</a></h2>
<p>If you can’t use ipython, and still want to use matplotlib/pylab from
an interactive python shell, e.g., the plain-ole standard python
interactive interpreter, you
are going to need to understand what a matplotlib backend is
<aclass="reference internal" href="../faq/usage_faq.html#what-is-a-backend"><em>What is a backend?</em></a>.</p>
<p>With the TkAgg backend, which uses the Tkinter user interface toolkit,
you can use matplotlib from an arbitrary non-gui python shell. Just set your
<ttclass="docutils literal"><spanclass="pre">backend</span><spanclass="pre">:</span><spanclass="pre">TkAgg</span></tt> and <ttclass="docutils literal"><spanclass="pre">interactive</span><spanclass="pre">:</span><spanclass="pre">True</span></tt> in your
<ttclass="file docutils literal"><spanclass="pre">matplotlibrc</span></tt> file (see <aclass="reference internal" href="customizing.html#customizing-matplotlib"><em>Customizing matplotlib</em></a>) and fire
<spanid="controlling-interactive"></span><h2>Controlling interactive updating<aclass="headerlink" href="#controlling-interactive-updating" title="Permalink to this headline">¶</a></h2>
<p>The <em>interactive</em> property of the pyplot interface controls whether a
figure canvas is drawn on every pyplot command. If <em>interactive</em> is
<em>False</em>, then the figure state is updated on every plot command, but
will only be drawn on explicit calls to
<aclass="reference internal" href="../api/pyplot_api.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><ttclass="xref py py-func docutils literal"><spanclass="pre">draw()</span></tt></a>. When <em>interactive</em> is
<em>True</em>, then every pyplot command triggers a draw.</p>
<p>The pyplot interface provides 4 commands that are useful for
<spanclass="gp">>>> </span><spanclass="n">title</span><spanclass="p">(</span><spanclass="s">'now how much would you pay?'</span><spanclass="p">)</span>