1

i have this working code for scatter animation in 2D:

import matplotlib.pyplot as plt
import matplotlib.animation as 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 to convert it into 3D with this but it wont work..

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from 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 someone please give me an advice on how to fix this? Thank you

1 Answer 1

1

_offsets3d is an attribute, not a method. Instead of

scat._offsets3d([0, 0, 0], [50, 0, 0], [100, 0, 0])

you will need to assign the tuple of (x,y,z) values to it:

scat._offsets3d = ([0, 0, 0], [50, 0, 0], [100, 0, 0])

This will of course always produce the same plot for all 100 frames. So in order to see an animation something like

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
from 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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.