Python的绘图方式包括“控制台绘图”和“弹出窗绘图”,动态绘图只能在弹出窗中进行,因此在绘图前必须进行设置。
案例1:
import numpy as np
import matplotlib.pyplot as plt
#%matplotlib auto #Jupyter notebook的弹出窗绘图语句,在Pycharm和Spyder中国必须删除该句
for i in range(10):
# () #绘制一个figure,标号基于前一个figure以自然数命名
# (i) #以指定数据作为标号绘制一个figure
plt.plot(np.random.randn(10,10)) #在当前figure中绘图,如果没有figure则自己新建一个再绘图
plt.pause(0.2) #暂停时间
plt.cla() #将当前figure中绘图区的内容清除
# () #将当前figure关闭
案例2:指定figure画图
import matplotlib.pyplot as plt
#%matplotlib auto #Jupyter notebook的弹出窗绘图语句,在Pycharm和Spyder中国必须删除该句
fig,ax=plt.subplots()
y1=[]
for i in range(50):
y1.append(i)
ax.cla()
ax.bar(y1,label='test',height=y1,width=0.3)
ax.legend()
plt.pause(0.3)
注意:要实现动态绘图,()是必须要的,否则图像不会展示(因为显示图像需要的时间比较长)!
python保存gif图像:/briblue/article/details/84940997