首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用matplotlib绘制实时图

无法使用matplotlib绘制实时图
EN

Stack Overflow用户
提问于 2017-07-14 19:34:10
回答 1查看 188关注 0票数 3

在在线搜索的帮助下,我编写了以下代码。我在这里的目的是得到一个实时图,其中x轴上有时间,y轴上有一些随机生成的值。

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

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

def animate(i):
    xar = []
    yar = []
    x,y = time.time(), np.random.rand()
    xar.append(x)
    yar.append(y)
    ax1.clear()
    ax1.plot(xar,yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show() 

使用上面的代码,我只看到y轴的范围不断变化,图形不会出现在图中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-14 19:49:20

问题是,您从未更新过xvaryvar。您可以通过将列表的定义移出animate的定义来做到这一点。

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

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
xar = []
yar = []

def animate(i):
    x,y = time.time(), np.random.rand()
    xar.append(x)
    yar.append(y)
    ax1.clear()
    ax1.plot(xar,yar)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45110424

复制
相关文章

相似问题

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