Search code examples
pythonmatplotlibenthoughtcanopy

matplotlib.figure() doesn't work when executing a script in Enthought Canopy IDE


When I execute the pyplot.figure() in the Python shell of the Enthought Canopy editor a graph menu is displayed. But when I run the following script, no graph menu is displayed?

from matplotlib import pyplot

def ex1():
    pyplot.figure()

if __name__ == "__main__":
    ex1()

Edit: I received the following message after re-executing the script a 'few' times. But I see not a single figure. C:\Users\Matthias\AppData\Local\Enthought\Canopy\User\lib\site-packages\matplotlib\pyplot.py:412: RuntimeWarning: More than 20 figures have been opened. Figures created through the pyplot interface (matplotlib.pyplot.figure) are retained until explicitly closed and may consume too much memory. (To control this warning, see the rcParam figure.max_num_figures). max_open_warning, RuntimeWarning)

Edit2: This seems to work fine.

import pylab

def ex1():
    pylab.figure()
    pylab.show()

if __name__ == "__main__":
    ex1()

Solution

  • show() is the final step of the normal programming sequence when plotting with matplotlib: assemble all your components, then show the result.

    In IPython running in pylab or matplotlib modes (Canopy, by default runs IPython in pylab mode), plot commands done directly at the IPython prompt are displayed one by one as you enter them. This is a special IPython feature to let you interact with your plots. (The "I" in IPython stands for interactive.)