How can I show figures separately in matplotlib?

后端 未结 7 1243
面向向阳花
面向向阳花 2020-11-29 18:58

Say that I have two figures in matplotlib, with one plot per figure:

import matplotlib.pyplot as plt

f1 = plt.figure()
plt.plot(range(0,10))
f2 = plt.figure         


        
7条回答
  •  一生所求
    2020-11-29 19:31

    I think I am a bit late to the party but... In my opinion, what you need is the object oriented API of matplotlib. In matplotlib 1.4.2 and using IPython 2.4.1 with Qt4Agg backend, I can do the following:

    import matplotlib.pyplot as plt
    fig, ax = plt.subplots(1) # Creates figure fig and add an axes, ax.
    fig2, ax2 = plt.subplots(1) # Another figure
    
    ax.plot(range(20)) #Add a straight line to the axes of the first figure.
    ax2.plot(range(100)) #Add a straight line to the axes of the first figure.
    
    fig.show() #Only shows figure 1 and removes it from the "current" stack.
    fig2.show() #Only shows figure 2 and removes it from the "current" stack.
    plt.show() #Does not show anything, because there is nothing in the "current" stack.
    fig.show() # Shows figure 1 again. You can show it as many times as you want.
    

    In this case plt.show() shows anything in the "current" stack. You can specify figure.show() ONLY if you are using a GUI backend (e.g. Qt4Agg). Otherwise, I think you will need to really dig down into the guts of matplotlib to monkeypatch a solution.

    Remember that most (all?) plt.* functions are just shortcuts and aliases for figure and axes methods. They are very useful for sequential programing, but you will find blocking walls very soon if you plan to use them in a more complex way.

提交回复
热议问题