这是一个使用 matplotlib 的动画功能自动更新的绘图示例。但是,您也可以在必要时自己调用更新例程:
import numpy as np
import matplotlib.pyplot as plt
import pandas
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
df = pandas.DataFrame(data=np.linspace(0, 100, 101), columns=["colA"])
fig = plt.figure()
ax = plt.gca()
ln, = ax.plot([], [], "o", mew=2, mfc="None", ms=15, mec="r")
class dataPlot(object):
def __init__(self):
self.objs = ax.plot(df.loc[0,"colA"], "g*", ms=15, mew=2, mec="g", mfc="None", label="$Data$")
fig.legend(self.objs, [l.get_label() for l in self.objs], loc="upper center", prop={"size":18}, ncol=2)
def update(self, iFrame):
for o in self.objs:
o.remove()
print("Rendering frame {:d}".format(iFrame))
self.objs = ax.plot(df.loc[iFrame,"colA"], "g*", ms=15, mew=2, mec="g", mfc="None", label="$Data$")
return ln,
dp = dataPlot()
ani = FuncAnimation(fig, dp.update, frames=df.index, blit=True)
plt.show()