class Animator: #@save """在动画中绘制数据""" def __init__(self, xlabel=None, ylabel=None, legend=None, xlim=None, ylim=None, xscale='linear', yscale='linear', fmts=('-', 'm--', 'g-.', 'r:'), nrows=1, ncols=1, figsize=(3.5, 2.5)): # 增量地绘制多条线 if legend is None: legend = [] d2l.use_svg_display() self.fig, self.axes = d2l.plt.subplots(nrows, ncols, figsize=figsize) if nrows * ncols == 1: self.axes = [self.axes, ] # 使⽤lambda函数捕获参数 self.config_axes = lambda: d2l.set_axes( self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend) self.X, self.Y, self.fmts = None, None, fmts def add(self, x, y): # 向图表中添加多个数据点 if not hasattr(y, "__len__"): y = [y] n = len(y) if not hasattr(x, "__len__"): x = [x] * n if not self.X: self.X = [[] for _ in range(n)] if not self.Y:
时间: 2023-11-18 20:04:46 浏览: 512
self.Y = [[] for _ in range(n)] for i, (a, b) in enumerate(zip(x, y)): self.X[i].append(a) self.Y[i].append(b) assert len(self.X[0]) == len(self.Y[0]) self.config_axes() for x, y, fmt in zip(self.X, self.Y, self.fmts): # 可视化数据 self.axes[0].plot(x, y, fmt) self.fig.canvas.draw()
阅读全文