matplotlib 画图功能非常强大,目前也只能根据官网提供的例子简单地画几张图。最近学习了能画动态图的animation模块,作个简单地记录。
在matplotlib作图中,比较常用的是matplotlib.pyplot模块,这个模块有非常多的属性和方法,简要列举下这次用到的方法:
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
返回fig和ax对象!
例子1. 动态画出sin函数曲线
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
xdata, ydata = [], []
ln, = ax.plot([], [], ‘r-‘, animated=False)
def init():
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1, 1)
return ln,
def update(frame):
xdata.append(frame)
ydata.append(np.sin(frame))
ln.set_data(xdata, ydata)
return ln,
ani = FuncAnimation(fig, update, frames=np.linspace(0, 2*np.pi, 128),
init_func=init, blit=True)
plt.show()
画这类图的关键是要给出不断更新的函数,这里就是update 函数了。注意, line, = ax.plot([], [], ‘r-‘, animated=False)
Original: https://blog.csdn.net/weixin_39561168/article/details/114910149
Author: weixin_39561168
Title: python animation 轨迹_Matplotlib animation模块实现动态图
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/767680/
转载文章受原作者版权保护。转载请注明原作者出处!