基础知识

plt绘制动态图

2019-12-25  本文已影响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()
上一篇 下一篇

猜你喜欢

热点阅读