Matplotlib - Force plot display and then return to main code

后端 未结 3 1503
心在旅途
心在旅途 2020-12-30 01:32

This is a MWE of what I\'m after, adapted from this question:

from matplotlib.pyplot import plot, draw, show

def make_plot():
    plot([1,2,3])
    draw()
          


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 02:07

    None of the presented solutions work for me. I tested them with three different IDEs PyCharm, Spyder and Pyzo, using the (currently) latest Matplotlib 2.1 under Python 3.6.

    What works for me, although not optimal, is to use a plt.pause command:

    import matplotlib.pyplot as plt
    
    def make_plot():
        plt.plot([1, 2, 3])
    #    plt.show(block=False)  # The plot does not appear.
    #    plt.draw()             # The plot does not appear.
        plt.pause(0.1)          # The plot properly appears.
        print('continue computation')
    
    print('Do something before plotting.')
    # Now display plot in a window
    make_plot()
    
    answer = input('Back to main and window visible? ')
    if answer == 'y':
        print('Excellent')
    else:
        print('Nope')
    

提交回复
热议问题