|
87 | 87 | #
|
88 | 88 | # Covering the set methods for all types of artists is beyond the scope of this
|
89 | 89 | # tutorial but can be found in their respective documentations. An example of
|
90 |
| -# such update methods in use for `.Axes.scatter` is as follows. |
91 |
| - |
| 90 | +# such update methods in use for `.Axes.scatter` and `.Axes.plot` is as follows. |
92 | 91 |
|
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 |
| - # .set_offsets replaces the offset data for the entire collection with |
105 |
| - # the new values. Therefore, to also carry forward the previously |
106 |
| - # calculated information, we use the data from the first to the current |
107 |
| - # frame to set the new offsets. |
108 |
| - x = np.sin(a * t[:frame] + delta) |
109 |
| - y = np.sin(b * t[:frame]) |
| 108 | + # for each frame, update the data stored on each artist. |
| 109 | + x = t[:frame] |
| 110 | + y = z[:frame] |
| 111 | + # update the scatter plot: |
110 | 112 | data = np.stack([x, y]).T
|
111 | 113 | scat.set_offsets(data)
|
112 |
| - return (scat,) |
| 114 | + # update the line plot: |
| 115 | + line2.set_xdata(t[:frame]) |
| 116 | + line2.set_ydata(z2[:frame]) |
| 117 | + return (scat, line2) |
113 | 118 |
|
114 | 119 |
|
115 |
| -ani = animation.FuncAnimation(fig=fig, func=update, frames=400, interval=30) |
| 120 | +ani = animation.FuncAnimation(fig=fig, func=update, frames=40, interval=30) |
116 | 121 | plt.show()
|
117 | 122 |
|
118 | 123 |
|
|
0 commit comments