Change dynamically the contents of a matplotlib plot

后端 未结 3 1925
灰色年华
灰色年华 2020-12-28 11:13

I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just

3条回答
  •  太阳男子
    2020-12-28 11:50

    Well I managed to do it with an event handler for mouse clicks. I will change it for something more useful, but I post my solution anyway.

    import matplotlib.pyplot as plt
    
    figure = plt.figure()
    # plotting
    plt.plot([1,2,3],[10,20,30],'bo-')
    plt.grid()
    plt.legend()
    
    def on_press(event):
        print 'you pressed', event.button, event.xdata, event.ydata
        event.canvas.figure.clear()
        # select new curves to plot, in this example [1,2,3] [0,0,0]
        event.canvas.figure.gca().plot([1,2,3],[0,0,0], 'ro-')
        event.canvas.figure.gca().grid()
        event.canvas.figure.gca().legend()
        event.canvas.draw()
    
    
    figure.canvas.mpl_connect('button_press_event', on_press)
    

提交回复
热议问题