开发者

python matplotlib -- regenerate graph?

I have a python function that generates a list with random values.

After I call this function, I call another function that plots the random values using matplotlib.

I want to be able to click some key on the keyboard / mouse, and have the following happen:

(1) a new list of random values will be re-generated

(2) the values from (1) will be plotted (replacing the current matplotlib chart)

Meaning, I want to be able to view new charts with a 开发者_开发百科click of a button. How do I go about doing this in python?


It's really quite easy to do with matplotlib. The basic idea is to use

plt.connect('button_press_event', onclick)

to call onclick whenever the user presses a button:

import matplotlib.pyplot as plt
import numpy as np

class Main(object):
    def clear(self):
        plt.clf()
    def redraw(self):
        self.clear()
        plt.plot(self.data)
        plt.title('100')
        plt.text(50,0.85,'100')
        plt.draw()
    def on_click(self,event):
        self.data=np.random.random(100)    
        self.redraw()        
    def run(self):
        plt.figure()
        plt.connect('button_press_event', self.on_click)
        plt.show() 

    def __init__(self):
        self.data=np.random.random(100)    

if __name__=='__main__':           
    m=Main()
    m.run()
    print(m.data)


from matplotlib.widgets import Button

real_points = plt.axes().scatter(x=xpts, y=ypts, alpha=.4, s=size, c='green', label='real data')

#Reset Button
#rect = [left, bottom, width, height]
reset_axis = plt.axes([0.4, 0.15, 0.1, 0.04])
button = Button(ax=reset_axis, label='Reset', color='lightblue' , hovercolor='0.975')

def reset(event):
real_points.remove()

button.on_clicked(reset)

plt.show()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜