Interactive Graphs

Interactive graphs, like the one shown in Figure 6-1, plot the graph in a separate window in Xor Windows. If you'd like this option, enter show() at the Python shell or call the function show() in a script.

Figure 6-1. Interactive graph

The function show() opens up an interactive window. Several notes about this window:

• The window is numbered, as you can see by the label "Figure 1." This is useful if you have several windows and would like subsequent plots to either override or appear on a specific figure. To switch between figures, use the figure(n) function, where n stands for the figure index. If you'd like a new figure, and don't particularly care about the figure index, issue the command figure(), which will create an empty figure with the next available index.

• The x-axis and y-axis were created automatically to fit the data. In a lot of the cases, matplotlib does an excellent job of automatically selecting the right axis (as in this example). However, if you want a different range of values to be displayed, that's doable with the axis() command, more on which appears later in the chapter in the section "Axis."

• The location of the mouse is printed on the right corner of the figure. This is very useful if you're trying to zoom in on data and find a specific data point. This functionality is not available when you plot graphs to file (i.e., noninteractive mode).

• You have several buttons on the lower-left side of the figure to allow interaction with the graph. The five leftmost buttons are used for zooming and zooming history. The first button from the left (with the house icon) is used to change axes to the original plot axes. The left and right arrow buttons cycle backward and forward through previous axes changes. The fourth button allows changing of the axes origin, and the fifth button from the left enables zooming. The sixth button from the left controls the margins of the plot in respect to the containing window, and lastly the seventh button allows saving the image to disk.

■ Note If you're not using matplotlib interactively in Python, be sure to call the function show() after all graphs have been generated, as it enters a user interface main loop that will stop execution of the rest of your code. The reason behind this behavior is that matplotlib is designed to be embedded in a GUI as well. In Windows, if you're working from interactive Python, you need only issue show() once; close the figures (or figures) to return to the shell. Subsequent plots will be drawn automatically without issuing show(), and you'll be able to plot graphs interactively.

Was this article helpful?

0 0

Post a comment