zoukankan      html  css  js  c++  java
  • matplotlib--animation

    1. 动图-animation

    import matplotlib.animation as animation
    
    n = 100
    x = np.random.randn(n)
    #**************************
    # create the function that will do the plotting, where curr is the current frame
    def update(curr):
        # check if animation is at the last frame, and if so, stop the animation a
        if curr == n: 
            a.event_source.stop()
        plt.cla()
        bins = np.arange(-4, 4, 0.5)
        plt.hist(x[:curr], bins=bins)
        plt.axis([-4,4,0,30])
        plt.gca().set_title('Sampling the Normal Distribution')
        plt.gca().set_ylabel('Frequency')
        plt.gca().set_xlabel('Value')
        plt.annotate('n = {}'.format(curr), [3,27])
    #*****************************
    fig = plt.figure()
    a = animation.FuncAnimation(fig, update, interval=100)

    2. 交互性(interactivity)

    Example1:

    plt.figure()
    data = np.random.rand(10)
    plt.plot(data)
    
    def onclick(event):
        plt.cla()
        plt.plot(data)
        plt.gca().set_title('Event at pixels {},{} 
    and data {},{}'.format(event.x, event.y, event.xdata, event.ydata))
    
    # tell mpl_connect we want to pass a 'button_press_event' into onclick when the event is detected
    plt.gcf().canvas.mpl_connect('button_press_event', onclick)

    Example2 :

    from random import shuffle
    origins = ['China', 'Brazil', 'India', 'USA', 'Canada', 'UK', 'Germany', 'Iraq', 'Chile', 'Mexico']
    
    shuffle(origins)
    
    df = pd.DataFrame({'height': np.random.rand(10),
                       'weight': np.random.rand(10),
                       'origin': origins})
    df

    作图:

    plt.figure()
    # picker=5 means the mouse doesn't have to click directly on an event, but can be up to 5 pixels away
    plt.scatter(df['height'], df['weight'], picker=5)
    plt.gca().set_ylabel('Weight')
    plt.gca().set_xlabel('Height')

    def onpick(event):
        origin = df.iloc[event.ind[0]]['origin']
        plt.gca().set_title('Selected item came from {}'.format(origin))
    
    # tell mpl_connect we want to pass a 'pick_event' into onpick when the event is detected
    plt.gcf().canvas.mpl_connect('pick_event', onpick)
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    牛客前缀和题、枚举---[HNOI2003]激光炸弹
    牛客贪心题---拼数
    牛客枚举题---明明的随机数
    牛客模拟、差分题---校门外的树
    牛客贪心题---纪念品分组
    03_7_继承和权限控制
    03_6_package和import语句
    03_5_static关键字
    01_8_sql主键生成方式
    01_7_模糊查询实体对象
  • 原文地址:https://www.cnblogs.com/Shinered/p/9531102.html
Copyright © 2011-2022 走看看