首页 / PYTHON / python – 动态过度绘图
python – 动态过度绘图
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 动态过度绘图,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1687字,纯文字阅读大概需要3分钟。
内容图文
![python – 动态过度绘图](/upload/InfoBanner/zyjiaocheng/809/4a21acf1a150433095f8110d5687c1db.jpg)
我需要绘制很多线条,并且我想在计算时显示它们.代码看起来像这样:
x = arange(100000)
for y in range(100000):
ax.plot(x*y)
draw()
现在,正如你可以想象的那样,这会很快变慢.我想我可以做的是绘图,将绘图保存到缓冲区,清除绘图,将缓冲区放置为背景,然后绘制下一行.这样,我最终没有这么多Line2D对象.有人会有任何想法吗?
解决方法:
看来你需要matplotlib.animation功能. animation examples.
EDIT: Added simpler example code of my own version.
import random
from matplotlib import pyplot as plt
from matplotlib import animation
def data_generator(t):
if t<100:
return random.sample(range(100), 20)
def init():
return plt.plot()
def animate(i):
data = data_generator(i)
return plt.plot(data, c='k')
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=1000, interval=1000, blit=True)
plt.show()
EDIT2: Multiple line live update version.
import random
from matplotlib import pyplot as plt
from matplotlib import animation
def data_generator_1(t):
if t<100:
x1.append(t)
y1.append(random.randint(1, 100))
def data_generator_2(t):
if t<100:
x2.append(t)
y2.append(random.randint(1, 100))
def init():
global x1
global y1
x1 = []
y1 = []
global x2
global y2
x2 = []
y2 = []
l1, l2 = plt.plot(x1, y1, x2, y2)
return l1, l2
def animate(i):
data_generator_1(i)
data_generator_2(i)
l1, l2 = plt.plot(x1, y1, x2, y2)
plt.setp(l1, ls='--', c='k')
plt.setp(l2, c='gray')
return l1, l2
fig = plt.figure()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=1000, interval=1000, blit=True)
plt.show()
我很确定有很多方法可以在没有全局变量的情况下绘制这种动画.这只是快速试用,向您展示您想要的可能性.
我不知道你的第一条评论所认为的ipython / vanilla脚本问题.所有示例都在普通编辑器(而不是ipython)上编码.也许有matplotlib版本的区别.
内容总结
以上是互联网集市为您收集整理的python – 动态过度绘图全部内容,希望文章能够帮你解决python – 动态过度绘图所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。