pythonmatplotlibplotwindow

In Python: Can I reverse the order in which several matplotlib plot windows are visible / in focus?


When I display several (say, 10 or so) matplotlib plots in either IDLE or VS Code, they generally appear as a cascading stack of plots from top left to bottom right. In this stack, the first plot is on the bottom and last plot is on top (that is, fully visible). To view any plot in the stack other than the last, I would click on its title bar, which is visible because the plots in the stack are cascaded, or staggered, from upper left to lower right.

The downside of this default behavior is that I (and plausibly other Python users) often want to view the first plot first and the last plot last. So it would be more convenient to have the visibility of the plots ordered from last to first, and to do so without changing the figure numbers (Figure 1 through Figure 10 if there are 10 figures).

As a hack, we might try to accomplish this in the code by reversing the order in which the figures are plotted, and also reversing the figure numbers. But this solution can be cumbersome — for readability, we may want to generate the plots at the place in the code where the plotted variables are constructed, rather than postponing all plotting to the end.

Is there a better solution to this problem?


Solution

  • The documentation states that pyplot.figure accepts an numerical argument as

    A unique identifier for the figure. If a figure with that identifier already exists, this figure is made active and returned.

    We can use that to reverse the order in the end after all figures are already created.

    import matplotlib.pyplot as plt
    
    for i in range(1, 11):
        plt.figure(i)  # creating the figure wit a unique identifier deep inside of the chart generating code
        plt.plot([i, 2*i, 3*i])
    
    # reversing the order at the end of your code
    for i in range(10, 0, -1):
        plt.figure(i)
    plt.show()