|
89 | 89 | # tutorial but can be found in their respective documentations. An example of
|
90 | 90 | # such update methods in use for `.Axes.scatter` is as follows.
|
91 | 91 |
|
92 |
| - |
93 | 92 | fig, ax = plt.subplots()
|
94 |
| -t = np.linspace(-np.pi, np.pi, 400) |
95 |
| -a, b = 3, 2 |
96 |
| -delta = np.pi / 2 |
| 93 | +t = np.linspace(0, 3, 40) |
| 94 | +g = -9.81 |
| 95 | +v0 = 12 |
| 96 | +z = g * t**2 / 2 + v0 * t |
| 97 | + |
| 98 | +v02 = 5 |
| 99 | +z2 = g * t**2 / 2 + v02 * t |
97 | 100 |
|
98 |
| -scat = ax.scatter(np.sin(a * t[0] + delta), np.sin(b * t[0]), c="b", s=2) |
99 |
| -ax.set_xlim(-1.5, 1.5) |
100 |
| -ax.set_ylim(-1.5, 1.5) |
| 101 | +scat = ax.scatter(t[0], z[0], c="b", s=5, label=f'v0 = {v0} m/s') |
| 102 | +line2 = ax.plot(t[0], z2[0], label=f'v0 = {v02} m/s')[0] |
| 103 | +ax.set(xlim= [0, 3], ylim=[-4, 10], xlabel='Time [s]', ylabel='Z [m]') |
| 104 | +ax.legend() |
101 | 105 |
|
102 | 106 |
|
103 | 107 | def update(frame):
|
104 | 108 | # .set_offsets replaces the offset data for the entire collection with
|
105 | 109 | # the new values. Therefore, to also carry forward the previously
|
106 | 110 | # calculated information, we use the data from the first to the current
|
107 | 111 | # frame to set the new offsets.
|
108 |
| - x = np.sin(a * t[:frame] + delta) |
109 |
| - y = np.sin(b * t[:frame]) |
| 112 | + x = t[:frame] |
| 113 | + y = z[:frame] |
110 | 114 | data = np.stack([x, y]).T
|
111 | 115 | scat.set_offsets(data)
|
112 |
| - return (scat,) |
| 116 | + line2.set_xdata(t[:frame]) |
| 117 | + line2.set_ydata(z2[:frame]) |
| 118 | + return (scat, line2) |
113 | 119 |
|
114 | 120 |
|
115 |
| -ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=300) |
| 121 | +ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30) |
116 | 122 | plt.show()
|
117 | 123 |
|
118 | 124 |
|
|
0 commit comments