Python 带有matplotlib-can';t支持/附加注释

Python 带有matplotlib-can';t支持/附加注释,python,animation,matplotlib,plot,Python,Animation,Matplotlib,Plot,我有一个动画python图。虽然每次计数器除以100时,我都能添加一个注释(箭头),但我不知道如何将箭头保持(或附加)到图形中(参见我的屏幕截图)。此时,add_注释起作用,但只持续1秒,然后它就消失了(应该至少再持续10秒左右) 改写了从 代码中有几个问题,主要问题是在动画的每个步骤中清除轴(ax1.clear()),因此删除注释 一种选择是保留一个包含所有注释的数组,并每次重新创建它们。这显然不是最优雅的解决方案 制作动画的正确方法是创建Line2D对象,并使用Line2D.set_dat

我有一个动画python图。虽然每次计数器除以100时,我都能添加一个注释(箭头),但我不知道如何将箭头保持(或附加)到图形中(参见我的屏幕截图)。此时,
add_注释
起作用,但只持续1秒,然后它就消失了(应该至少再持续10秒左右)

改写了从


代码中有几个问题,主要问题是在动画的每个步骤中清除轴(
ax1.clear()
),因此删除注释

一种选择是保留一个包含所有注释的数组,并每次重新创建它们。这显然不是最优雅的解决方案

制作动画的正确方法是创建
Line2D
对象,并使用
Line2D.set_data()
Line2D.set_xdata()
Line2D.set_ydata()
更新直线绘制的坐标

请注意,动画功能应返回已修改的艺术家列表(至少如果您希望使用blitting,这将提高性能)。因此,创建注释时,需要返回
注释
对象,并将其保留在列表中,然后通过动画功能返回所有艺术家

这很快就组合起来了,但应该为您提供一个起点:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation 

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

ax1.set_xlim((0,500))
ax1.set_ylim((0,200))

z = np.random.normal(0,1,255)
u = 0.1
sd = 0.3
counter = 0
price = [100]
t = [0]
artists = []
l, = plt.plot([],[],color="blue")


def add_annotation(annotated_message,value):
    annotation = plt.annotate(annotated_message, 
                 xy = (counter, value), xytext = (counter-5, value+20),
                 textcoords = 'offset points', ha = 'right', va = 'bottom',
                 bbox = dict(boxstyle = 'round,pad=0.1', fc = 'yellow', alpha = 0.5),
                 arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
    return annotation


def getNewPrice(s,mean,stdev):
    r = np.random.normal(0,1,1)
    priceToday = s + s*(mean/255+stdev/np.sqrt(225)*r)
    return priceToday


def animate(i):
    global t,u,sd,counter,artists
    x = t
    y = price
    counter += 1
    x.append(counter)
    value = getNewPrice(price[counter-1],u,sd)
    y.append(value)
    l.set_data(x,y)

    if counter%100==0:
        print(artists)
        new_annotation = add_annotation("100",value)
        artists.append(new_annotation)

    return [l]+artists


ani = animation.FuncAnimation(fig,animate,interval=20,frames=500)

代码中有几个问题,主要问题是在动画的每个步骤中清除轴(
ax1.clear()
),因此删除注释

一种选择是保留一个包含所有注释的数组,并每次重新创建它们。这显然不是最优雅的解决方案

制作动画的正确方法是创建
Line2D
对象,并使用
Line2D.set_data()
Line2D.set_xdata()
Line2D.set_ydata()
更新直线绘制的坐标

请注意,动画功能应返回已修改的艺术家列表(至少如果您希望使用blitting,这将提高性能)。因此,创建注释时,需要返回
注释
对象,并将其保留在列表中,然后通过动画功能返回所有艺术家

这很快就组合起来了,但应该为您提供一个起点:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation 

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

ax1.set_xlim((0,500))
ax1.set_ylim((0,200))

z = np.random.normal(0,1,255)
u = 0.1
sd = 0.3
counter = 0
price = [100]
t = [0]
artists = []
l, = plt.plot([],[],color="blue")


def add_annotation(annotated_message,value):
    annotation = plt.annotate(annotated_message, 
                 xy = (counter, value), xytext = (counter-5, value+20),
                 textcoords = 'offset points', ha = 'right', va = 'bottom',
                 bbox = dict(boxstyle = 'round,pad=0.1', fc = 'yellow', alpha = 0.5),
                 arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
    return annotation


def getNewPrice(s,mean,stdev):
    r = np.random.normal(0,1,1)
    priceToday = s + s*(mean/255+stdev/np.sqrt(225)*r)
    return priceToday


def animate(i):
    global t,u,sd,counter,artists
    x = t
    y = price
    counter += 1
    x.append(counter)
    value = getNewPrice(price[counter-1],u,sd)
    y.append(value)
    l.set_data(x,y)

    if counter%100==0:
        print(artists)
        new_annotation = add_annotation("100",value)
        artists.append(new_annotation)

    return [l]+artists


ani = animation.FuncAnimation(fig,animate,interval=20,frames=500)