<divid="unreleased-message"> You are reading an old version of the documentation (v2.1.2). 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>
<divclass="admonition warning">
<pclass="first admonition-title">Warning</p>
<pclass="last">This page is significantly out of date</p>
</div>
<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, e.g., after changing the
<aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.xlabel.html#matplotlib.pyplot.xlabel" title="matplotlib.pyplot.xlabel"><codeclass="xref py py-func docutils literal"><spanclass="pre">xlabel()</span></code></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>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">The mode described here still exists for historical reasons, but it is
highly advised not to use. It pollutes namespaces with functions that will
shadow python built-in and can lead to hard to track bugs. To get IPython
integration without imports the use of the <codeclass="xref py py-obj docutils literal"><spanclass="pre">%matplotlib</span></code> magic is
<p>it sets everything up for you so interactive plotting works as you
would expect it to. Call <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure" title="matplotlib.pyplot.figure"><codeclass="xref py py-func docutils literal"><spanclass="pre">figure()</span></code></a> and a
figure window pops up, call <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot" title="matplotlib.pyplot.plot"><codeclass="xref py py-func docutils literal"><spanclass="pre">plot()</span></code></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 <codeclass="docutils literal"><spanclass="pre">run</span></code> command to run matplotlib scripts
efficiently. ipython will turn off interactive mode during a <codeclass="docutils literal"><spanclass="pre">run</span></code>
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="https://mail.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="../tutorials/introductory/usage.html#what-is-a-backend"><spanclass="std std-ref">What is a backend?</span></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
<codeclass="docutils literal"><spanclass="pre">backend</span><spanclass="pre">:</span><spanclass="pre">TkAgg</span></code> and <codeclass="docutils literal"><spanclass="pre">interactive</span><spanclass="pre">:</span><spanclass="pre">True</span></code> in your
<codeclass="file docutils literal"><spanclass="pre">matplotlibrc</span></code> file (see <aclass="reference internal" href="../tutorials/introductory/customizing.html#sphx-glr-tutorials-introductory-customizing-py"><spanclass="std std-ref">Customizing matplotlib</span></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/_as_gen/matplotlib.pyplot.draw.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><codeclass="xref py py-func docutils literal"><spanclass="pre">draw()</span></code></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="s1">'now how much would you pay?'</span><spanclass="p">)</span>