The Matplotlib library is designed to work well with many different environments and platforms. As such, the library does not only contain routines for generating graphs, but it also contains support for displaying graphs in different graphical environments. To this end, Matplotlib provides backends for generating graphics in different formats (e.g., PNG, PDF, Postscript, and SVG) and for displaying graphics in a graphical user interface using a variety of different widget toolkits (e.g., Qt, GTK, wxWidgets, and Cocoa for Mac OS X) that are suitable for different platforms. Which backend to use can be selected in that Matplotlib resource file,2 or using the function mpl.use, which must be called right after importing matplotlib, before importing the matplotlib.pyplot module. For example, to select the Qt4Agg backend, we can use:
import matplotlib as mpl
mpl.use('qt4agg')
import matplotlib.pyplot as plt
The Matplotlib resource file, matplotlibrc, can be used to set default values of many Matplotlib parameters, including which backend to use. The location of the file is platform dependent. For details, see http://matplotlib.org/users/customizing.html.
The graphical user interface for displaying Matplotlib figures, is useful for interactive use with Python script files or the IPython console, and it allows to interactively explore figures, for example, by zooming and panning. When using an interactive backend, which displays the figure in a graphical user interface, it is necessary to call the function plt.show to get the window to appear on the screen. By default, the plt.show call will hang until the window is closed. For a more interactive experience, we can activate interactive mode by calling the function plt.ion. This instructs Matplotlib to take over the GUI event loop and show a window for a figure as soon as it is created, returning the control flow to the Python or IPython interpreter. To have the changes to a figure take effect, we need to issue a redraw command using the function plt.draw. We can deactivate the interactive mode using the function plt.ioff, and we can use the function mpl.is_interactive to check if Matplotlib is in interactive or noninteractive mode.