2Real-time rendering using MatPlotLib.
8import matplotlib.pyplot
as plt
26 matplotlib.rcParams[
"toolbar"] =
"None"
27 self.
fig = plt.figure()
28 self.
ax = self.
fig.add_subplot()
33 self.
ax.set_xlim(xlim)
34 self.
ax.set_ylim(ylim)
35 self.
ax.set_aspect(
"equal", adjustable=
"box")
37 self.
ax.set_title(title)
38 self.
ax.set_xlabel(xlabel)
39 self.
ax.set_ylabel(ylabel)
42 self.
ax.set_axis_off()
47 self.
fig.tight_layout(pad=0)
48 w, h = self.
fig.get_size_inches()
49 x1, x2 = self.
ax.get_xlim()
50 y1, y2 = self.
ax.get_ylim()
51 self.
fig.set_size_inches(
52 2 * 1.08 * w, 2 * self.
ax.get_aspect() * (y2 - y1) / (x2 - x1) * w
64 self.
bg = self.
fig.canvas.copy_from_bbox(self.
fig.bbox)
67 (points,) = self.
ax.plot([], [], m, alpha=alpha, animated=
True, label=label)
71 self.
fig.canvas.restore_region(self.
bg)
75 raise ValueError(f
"Plot {label} not registered")
81 self.
ax.draw_artist(self.
ax.legend())
82 self.
fig.canvas.blit(self.
fig.bbox)
83 self.
fig.canvas.flush_events()
89if __name__ ==
"__main__":
91 pp.register_plot(
"dots")
92 pp.register_plot(
"lines", m=
"-")
98 for i
in range(frame_count):
99 t = (2 * np.pi / num_dots) * np.arange(num_dots)
102 x = np.cos(t) * np.cos(4 * t)
103 y = np.sin(t) * np.cos(4 * t)
106 pp.draw_points(
"dots", x, y)
107 pp.draw_points(
"lines", x, y)
110 print(f
"Average FPS: {frame_count / (time.time() - tic)}")
end_drawing(self, delay=0)
__init__(self, title=None, xlabel=None, ylabel=None, xlim=[-1.5, 1.5], ylim=None, hide_axis=False, tight=False, enable_grid=False, enable_legend=False)
register_plot(self, label, m="o", alpha=1)
draw_points(self, label, x, y)