python - Scatter plot Matplotlib 2D > 3D -
i have working code scatter animation in 2d:
import matplotlib.pyplot plt import matplotlib.animation animation def _update_plot(i, fig, scat): scat.set_offsets(([0, i], [50, i], [100, i])) return scat, fig = plt.figure() x = [0, 50, 100] y = [0, 0, 0] ax = fig.add_subplot(111) ax.set_xlim([-50, 200]) ax.set_ylim([-50, 200]) scat = plt.scatter(x, y, c=x) scat.set_alpha(0.8) anim = animation.funcanimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) plt.show()
i tried convert 3d wont work..
import matplotlib.pyplot plt import matplotlib.animation animation import numpy np mpl_toolkits.mplot3d import axes3d def _update_plot(i, fig, scat): scat._offsets3d([0, 0, 0], [50, 0, 0], [100, 0, 0]) return scat fig = plt.figure() x = [0, 50, 100] y = [0, 0, 0] z = [0, 0, 0] ax = fig.add_subplot(111, projection='3d') scat = ax.scatter(x, y, z) anim = animation.funcanimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) plt.show()
can please give me advice on how fix this? thank you
_offsets3d
attribute, not method. instead of
scat._offsets3d([0, 0, 0], [50, 0, 0], [100, 0, 0])
you need assign tuple of (x,y,z)
values it:
scat._offsets3d = ([0, 0, 0], [50, 0, 0], [100, 0, 0])
this of course produce same plot 100 frames. in order see animation like
import matplotlib.pyplot plt import matplotlib.animation animation import numpy np mpl_toolkits.mplot3d import axes3d def _update_plot(i, fig, scat): scat._offsets3d = ([0, i, i], [50, i, 0], [100, 0, i]) return scat fig = plt.figure() x = [0, 50, 100] y = [0, 0, 0] z = [0, 0, 0] ax = fig.add_subplot(111, projection='3d') scat = ax.scatter(x, y, z) ax.set_xlim(0,100) ax.set_ylim(0,100) ax.set_zlim(0,100) anim = animation.funcanimation(fig, _update_plot, fargs=(fig, scat), frames=100, interval=100) plt.show()
Comments
Post a Comment