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.
  • 相关阅读:
    java。多态
    java。构造方法
    java.final修饰符l
    java。this的用法
    数据库:内连接与外连接区别
    Java工具类-设置字符编码
    Java工具类-验证码工具
    Java工具类-加密算法
    java中的object... args参数
    针对MySql封装的JDBC通用框架类(包含增删改查、JavaBean反射原理)
  • 原文地址:https://www.cnblogs.com/Shinered/p/9531102.html
Copyright © 2011-2022 走看看