使用matplotlib动态刷新指定曲线实例

我就废话不多说啦,还是直接看代码吧!
from matplotlib import pyplot as plt
import numpy as np
x = np.linspace(1, 100, 20)
y = x *2 +3
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x, y)
plt.ion()
for i in range(10):
y = x*i*0.1 + i
ax.lines.remove(lines[0])
except Exception:
lines = ax.plot(x ,y)
plt.pause(0.1)
补充知识:用Python的matplotlib库动态显示不断增长的数据
"""
Created on Mon Dec 07 16:34:10 2015
@author: SuperWang
import matplotlib.pyplot as plt
import numpy as np
fig,ax=plt.subplots()
fig2,ax2=plt.subplots()
y1=[]
y2=[]
for i in range(50):
y1.append(np.sin(i))
y2.append(np.cos(i))
ax.cla()
ax.set_title("Loss")
ax.set_xlabel("Iteration")
ax.set_ylabel("Loss")
ax.set_xlim(0,55)
ax.set_ylim(-1,1)
ax.grid()
ax.plot(y1,label='train')
ax.plot(y2,label='test')
ax.legend(loc='best')
ax2.cla()
ax2.set_title("Loss")
ax2.set_xlabel("Iteration")
ax2.set_ylabel("Loss")
ax2.set_xlim(0,55)
ax2.set_ylim(-1,1)