How to show two figures using matplotlib?

后端 未结 4 564
甜味超标
甜味超标 2020-11-29 18:02

I have some troubles while drawing two figures at the same time, not shown in a single plot. But according to the documentation, I wrote the code and only the figure one sho

相关标签:
4条回答
  • 2020-11-29 18:42

    Alternatively to calling plt.show() at the end of the script, you can also control each figure separately doing:

    f = plt.figure(1)
    plt.hist........
    ............
    f.show()
    
    g = plt.figure(2)
    plt.hist(........
    ................
    g.show()
    
    raw_input()
    

    In this case you must call raw_input to keep the figures alive. This way you can select dynamically which figures you want to show

    Note: raw_input() was renamed to input() in Python 3

    0 讨论(0)
  • 2020-11-29 18:44

    Alternatively, I would suggest turning interactive on in the beginning and at the very last plot, turn it off. All will show up, but they will not disappear as your program will stay around until you close the figures.

    import matplotlib.pyplot as plt
    from matplotlib import interactive
    
    plt.figure(1)
    ... code to make figure (1)
    
    interactive(True)
    plt.show()
    
    plt.figure(2)
    ... code to make figure (2)
    
    plt.show()
    
    plt.figure(3)
    ... code to make figure (3)
    
    interactive(False)
    plt.show()
    
    0 讨论(0)
  • 2020-11-29 19:00

    You should call plt.show() only at the end after creating all the plots.

    0 讨论(0)
  • 2020-11-29 19:03

    I had this same problem.


    Did:

    f1 = plt.figure(1)
    
    # code for figure 1
    
    # don't write 'plt.show()' here
    
    
    f2 = plt.figure(2)
    
    # code for figure 2
    
    plt.show()
    


    Write 'plt.show()' only once, after the last figure. Worked for me.

    0 讨论(0)
提交回复
热议问题