美文网首页基础知识
plt绘制动态图

plt绘制动态图

作者: 红豆汤来两大碗 | 来源:发表于2019-12-25 21:45 被阅读0次

1、使用ion,一般绘制折线图

import matplotlib.pyplot as plt
import numpy as np

plt.ion()
step =10
for i in range(10000):
    if i%step == 0:
        plt.cla()
        x = np.arange(0, 10, 0.1)
        y = np.random.randint(0, 100, 100)
        plt.plot(x, y, label='path', linewidth=1, color='r', marker='o', markerfacecolor='blue',
                                 markersize=6)
        try:
            plt.pause(0.1)
        except:
            break
plt.ioff()
plt.show()

2、animation包,封装更全,其中func还有一些参数没有用,无所谓吧。

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

fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

def update(i):
    line.set_ydata(np.sin(x+i/10))
    plt.setp(line, 'color', 'c', 'linewidth', 2.0)

ani = animation.FuncAnimation(fig, update, frames=None, interval=20)
plt.show()

相关文章

网友评论

    本文标题:plt绘制动态图

    本文链接:https://www.haomeiwen.com/subject/opehoctx.html