首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用matplotlib的动画图形-无法维护/附加注释

使用matplotlib的动画图形-无法维护/附加注释
EN

Stack Overflow用户
提问于 2017-04-19 02:59:48
回答 1查看 667关注 0票数 1

我有一个动画的蟒蛇图形。虽然我可以在每次计数器被100整除时添加一个注释(箭头),但我不知道如何将箭头保持(或附加)到图中(参见我的屏幕截图)。目前,add_annotation只工作了1秒,然后就消失了(它应该至少会继续显示10秒左右)。

改述了取自here的示例

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation 

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

z = np.random.normal(0,1,255)
u = 0.1
sd = 0.3
counter = 0
price = [100]
t = [0]

def add_annotation(annotated_message,value):
    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'))

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
    x = t
    y = price
    counter += 1
    x.append(counter)
    value = getNewPrice(price[counter-1],u,sd)
    y.append(value)
    ax1.clear()

    if counter%100==0:
        print "ping"
        add_annotation("100",value)

    plt.plot(x,y,color="blue")


ani = animation.FuncAnimation(fig,animate,interval=20)
plt.show()
EN

回答 1

Stack Overflow用户

发布于 2017-04-19 17:19:30

您的代码有几个问题,主要的问题是您在动画(ax1.clear())的每一步都清除了轴,因此您正在删除注释。

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

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

请注意,动画函数应返回已修改的美工人员列表(至少如果您想要使用blitting,这将提高性能)。因此,当您创建注释时,您需要返回Annotation对象,并将它们保存在列表中,并通过动画函数返回所有艺术家。

这是快速组合在一起的,但应该给你一个起点:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43480195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档