<type>how-to</type>
<domain>install</domain>
<id>matplotlib</id>
<title>Using Wing with matplotlib</title>
<date>$Date: 2006-12-28 17:21:05 -0500 (Thu, 28 Dec 2006) $</date>
<product>per+</product>
<summary>Learn how Wing can be used to speed up development and debugging of Python code written for matplotlib.</summary>

.. image:: /images/screenshots/wing7-screenshot-linen.png
  :alt: Wing Screen Shot
  :width: 574
  :height: 423
  :popup: 1

`Wing <https://wingware.com>`__ is a Python IDE that can be used to develop, test, and
debug Python code written for `matplotlib <https://matplotlib.org/>`__, a
powerful plotting library. 

Two versions of Wing are appropriate for use with this document: Wing Pro is the
full-featured Python IDE for professional developers, and Wing Personal is a free
alternative with reduced feature set.

If you do not already have Wing installed, `download it now <https://wingware.com/downloads>`__.

This document describes how to configure Wing for matplotlib. To get started using Wing as
your Python IDE, please refer to the tutorial in Wing's ``Help`` menu or read the
`Quickstart Guide <ref:howtos/quickstart>`__.

Working in the Python Shell
---------------------------

Users of matplotlib often work interactively in the Python command line shell. For
example, two plots could be shown in succession by typing the following::

  from pylab import plot,show,close
  x = range(10)
  plot(x)
  show()
  y = [2, 8, 3, 9, 4]
  plot(y)

In some environments, the ``show()`` call above will block until the plot window is
closed. To avoid this, Wing modifies the matplotlib event loop in such a way that the
``show()`` call will not block when entered in the integrated `Python Shell
<script:show_panel_python_shell>`__, and the plot window will be updated continuously as
additional commands are typed. In fact ``show()`` is not needed at all here since Wing
automatically shows and updates plots once ``plot()`` is called, but calling it is not a
problem.

Code from the editor can be executed in the Python Shell using the ``Evaluate File in
Python Shell`` item in the ``Source`` menu or with the ``Evaluate Selection in Python
Shell`` item in the editor context menu (right click). By default the ``Python Shell``
restarts before evaluating a whole file; this can be disabled in the Python Shell's
``Options`` menu.

This special event loop support has been implemented for the ``TkAgg``, ``GTKAgg``,
``GtkCairo``, ``WXAgg`` (for wxPython 2.5+), ``Qt5Agg``, ``Qt4Agg``, and ``MacOSX``
backends. It will not work with other backends.

Working in the Debugger
-----------------------

When executing code that includes ``show()`` in the debugger, Wing will block within the
``show()`` call just as Python would outside of the debugger if launched on the same file.
This is by design, since the debugger seeks to replicate Python run non-interactively.

To work interactively with matplotlib code launched in the debugger set a breakpoint where
you want to stop, and then work in the `Debug Console <script:show_panel_debug_console>`__.
Type ``show()`` in the ``Debug Console`` if your plot is not yet visible, or call
``matplotlib.interactive(True)`` to put matplotlib into interactive mode so that calling
``show()`` is no longer necessary.

Wing adds an item ``Evaluate Selection in Debug Console`` to the editor's right-click
context menu when the debugger is active.

Trouble-shooting
----------------

If ``show()`` blocks when typed in the ``Python Shell`` or ``Debug Console``, if plots fail
to update, or if you run into other event loop problems working with matplotlib you can:

(1) When working in the ``Debug Console``, evaluate the imports that set up matplotlib
first, so that Wing can initialize its event loop support before ``show()`` is called.
Evaluating a whole file at once in the ``Debug Console`` (but not the ``Python Shell``) will
cause ``show()`` to block if matplotlib was not previously imported.

(2) In case there is a problem with the specific matplotlib backend that you are using,
try the following as a way to switch to another backend before issuing any other
commands::

  import matplotlib
  matplotlib.use('TkAgg')
  
(3) Try disabling the matplotlib support entirely in ``Project Properties`` under the
``Extensions`` tab and then restart the ``Python Shell`` from its ``Options`` menu and
restart your debug process, if any. However, this prevents interactive use of matplotlib
in the ``Python Shell`` and ``Debug Console``.

Please email support@wingware.com if you run into problems that you cannot resolve.

Related Documents
-----------------

For more information see:

* `The matplotlib website <https://matplotlib.org/>`__

* `Quickstart Guide <ref:howtos/quickstart>`__ contains additional basic information
  about getting started with Wing.
  
* `Tutorial <ref:intro/tutorial>`__ provides a gentler introduction to Wing's features.

* `Wing Reference Manual <ref:manual>`__ documents Wing in detail.
