animated 3d python plot with several moving points - python

I have a similar problem to this one:
Animate a python pyplot by moving a point plotted via scatter.
The given values are positions (q) with corresponding velocities (v). Now I want to animate the shift in position over time for each point. My attempt
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
fig = plt.figure()
ax = p3.Axes3D(fig)
q = [[-4.32, -2.17, -2.25, 4.72, 2.97, 1.74],
[ 2.45, 9.73, 7.45,4.01,3.42, 1.80],[-1.40, -1.76, -3.08,-9.94,-3.13,-1.13]]
v = [[ 0.0068,0.024, -0.014,-0.013, -0.0068,-0.04],[ 0.012,
0.056, -0.022,0.016, 0.0045, 0.039],
[-0.0045, 0.031, 0.077,0.0016, -0.015,-0.00012]]
t=np.arange(0, 1000, 2)
x=q[0]
y=q[1]
z=q[2]
s=v[0]
u=v[1]
w=v[2]
points, = ax.plot(x, y, z, '*')
def update_points(t, x, y, z, points):
point = []
for i in range(0,len(x)-1,1):
points.set_data(np.array([x[i]+s[i]*t*5, y[i]+u[i]*t*5]))
points.set_3d_properties(z[i]+w[i]*t*5, 'z')
return point
ani=animation.FuncAnimation(fig, update_points, 10, fargs=(x, y, z, points))
plt.show()
does not work properly. The first image of the animation shows all points, but afterward, only the movement of the last point is simulated. The error happens in the defined update_points function because it seems like only the values for the last I are stored. Does anybody know how I have to change the code that all points are moving simultaneously?

Your problem is that you are only passing one set of coordinates to set_data()/set_3d_properties, and therefore only one point remains. You need to update all the coordinates of all your points, and then update points with those arrays.
I could not figure out exactly how you were doing your math in the update function, so here's an example using random fluctuations:
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
fig = plt.figure()
ax = p3.Axes3D(fig)
q = [[-4.32, -2.17, -2.25, 4.72, 2.97, 1.74],
[ 2.45, 9.73, 7.45,4.01,3.42, 1.80],[-1.40, -1.76, -3.08,-9.94,-3.13,-1.13]]
v = [[ 0.0068,0.024, -0.014,-0.013, -0.0068,-0.04],[ 0.012,
0.056, -0.022,0.016, 0.0045, 0.039],
[-0.0045, 0.031, 0.077,0.0016, -0.015,-0.00012]]
x=np.array(q[0])
y=np.array(q[1])
z=np.array(q[2])
s=np.array(v[0])
u=np.array(v[1])
w=np.array(v[2])
points, = ax.plot(x, y, z, '*')
txt = fig.suptitle('')
def update_points(num, x, y, z, points):
txt.set_text('num={:d}'.format(num)) # for debug purposes
# calculate the new sets of coordinates here. The resulting arrays should have the same shape
# as the original x,y,z
new_x = x+np.random.normal(1,0.1, size=(len(x),))
new_y = y+np.random.normal(1,0.1, size=(len(y),))
new_z = z+np.random.normal(1,0.1, size=(len(z),))
# update properties
points.set_data(new_x,new_y)
points.set_3d_properties(new_z, 'z')
# return modified artists
return points,txt
ani=animation.FuncAnimation(fig, update_points, frames=10, fargs=(x, y, z, points))
plt.show()

Thank you very much! It works now
from matplotlib import pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d.axes3d as p3
from matplotlib import animation
fig = plt.figure()
ax = p3.Axes3D(fig)
q = [[-4.32, -2.17, -2.25, 4.72, 2.97, 1.74],
[ 2.45, 9.73, 7.45,4.01,3.42, 1.80],[-1.40, -1.76, -3.08,-9.94,-3.13,-1.13]]
v = [[ 0.0068,0.024, -0.014,-0.013, -0.0068,-0.04],[ 0.012,
0.056, -0.022,0.016, 0.0045, 0.039],
[-0.0045, 0.031, 0.077,0.0016, -0.015,-0.00012]]
x=np.array(q[0])
y=np.array(q[1])
z=np.array(q[2])
s=np.array(v[0])
u=np.array(v[1])
w=np.array(v[2])
points, = ax.plot(x, y, z, '*')
txt = fig.suptitle('')
def update_points(t, x, y, z, points):
txt.set_text('num={:d}'.format(t))
new_x = x + s * t
new_y = y + u * t
new_z = z + w * t
print('t:', t)
# update properties
points.set_data(new_x,new_y)
points.set_3d_properties(new_z, 'z')
# return modified artists
return points,txt
ani=animation.FuncAnimation(fig, update_points, frames=15, fargs=(x, y, z, points))
ax.set_xlabel("x [pc]")
ax.set_ylabel("y [pc]")
ax.set_zlabel('z [pc]')
plt.show()

Related

Changing the position of x-y plane [duplicate]

I am using mplot3d from the mpl_toolkits library. When displaying the 3D surface on the figure I'm realized the axis were not positioned as I wished they would.
Let me show, I have added to the following screenshot the position of each axis:
Is there a way to change the position of the axes in order to get this result:
Here's the working code:
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(plt.figure())
def f(x,y) :
return -x**2 - y**2
X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)
# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])
ax.set_yticklabels([-1,1],rotation=-15, va='center', ha='right')
plt.show()
I have tried using xaxis.set_ticks_position('left') statement, but it doesn't work.
No documented methods, but with some hacking ideas from https://stackoverflow.com/a/15048653/1149007 you can.
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = ax = fig.add_subplot(111, projection='3d')
ax.view_init(30, 30)
def f(x,y) :
return -x**2 - y**2
X = np.arange(-1, 1, 0.02)
Y = np.arange(-1, 1, 0.02)
X, Y = np.meshgrid(X, Y)
Z = f(X, Y)
ax.plot_surface(X, Y, Z, alpha=0.5)
# Hide axes ticks
ax.set_xticks([-1,1])
ax.set_yticks([-1,1])
ax.set_zticks([-2,0])
ax.xaxis._axinfo['juggled'] = (0,0,0)
ax.yaxis._axinfo['juggled'] = (1,1,1)
ax.zaxis._axinfo['juggled'] = (2,2,2)
plt.show()
I can no idea of the meaning of the third number in triples. If set zeros nothing changes in the figure. So should look in the code for further tuning.
You can also look at related question Changing position of vertical (z) axis of 3D plot (Matplotlib)? with low level hacking of _PLANES property.
Something changed, code blow doesn't work, all axis hide...
ax.xaxis._axinfo['juggled'] = (0,0,0)
ax.yaxis._axinfo['juggled'] = (1,1,1)
ax.zaxis._axinfo['juggled'] = (2,2,2)
I suggest using the plot function to create a graph

Reducing borders in matplotlib quiver

I'm willing to plot a simple vector field using the following code:
import numpy
import matplotlib.pyplot as plt
def plot_quiver(vx, vy, fp):
plt.figure()
x_steps, y_steps = vx.shape
x, y = numpy.meshgrid(numpy.linspace(0, x_steps, x_steps), numpy.linspace(0, y_steps, y_steps))
m = numpy.sqrt(numpy.power(vx, 2) + numpy.power(vy, 2))
fig = plt.quiver(x, y, vx, vy, m)
plt.axes().set_aspect('equal')
l, r, b, t = plt.axis()
dx, dy = r-l, t-b
plt.axis([l-0.1*dx, r+0.1*dx, b-0.1*dy, t+0.1*dy])
plt.savefig(fp, format='pdf', bbox_inches='tight')
plt.show()
plt.close()
What I don't get is why I have wider top and right borders, and how to adjust it.
matplotlib seems to get the next round number after the size of your vector field.
Here r, t = 70, 70.
To make sure your problem does not depend on the scaling of matplotlib, use
plt.axis([-0.1*x_steps, 1.1*x_steps, -0.1*y_steps, 1.1*y_steps])

How to plot a vector field over a contour plot in matplotlib?

I've managed to plot a vector field and a contour plot and would like to display both of them on top of each other, I've looked around but don't quite understand how figure and subplots work. Here is my code:
from matplotlib.pyplot import cm
import numpy as np
import matplotlib.pyplot as plt
# Vector Field
Y, X = np.mgrid[-2:2:20j, -2:2:20j]
U =(1 - 2*(X**2))*np.exp(-((X**2)+(Y**2)))
V = -2*X*Y*np.exp(-((X**2)+(Y**2)))
speed = np.sqrt(U**2 + V**2)
UN = U/speed
VN = V/speed
plt.quiver(X, Y, UN, VN,
color='Teal',
headlength=7)
plt.show()
# Countour Plot
X, Y = np.mgrid[-2:2:100j, -2:2:100j]
Z = X*np.exp(-(X**2 + Y**2))
cp = plt.contourf(X, Y, Z)
plt.colorbar(cp)
plt.show()
You have two issues:
Calling plt.show() in between the plots: this makes them separate figures, instead of overlaying one on the other
Plotting the quiver plot before the contour plot: so even if you removed the show(), the contour plot would cover up the quiver.
Simple fix!
from matplotlib.pyplot import cm
import numpy as np
import matplotlib.pyplot as plt
# Contour Plot
X, Y = np.mgrid[-2:2:100j, -2:2:100j]
Z = X*np.exp(-(X**2 + Y**2))
cp = plt.contourf(X, Y, Z)
cb = plt.colorbar(cp)
# Vector Field
Y, X = np.mgrid[-2:2:20j, -2:2:20j]
U =(1 - 2*(X**2))*np.exp(-((X**2)+(Y**2)))
V = -2*X*Y*np.exp(-((X**2)+(Y**2)))
speed = np.sqrt(U**2 + V**2)
UN = U/speed
VN = V/speed
quiv = plt.quiver(X, Y, UN, VN, # assign to var
color='Teal',
headlength=7)
plt.show()
Result:

How to plot a point inside a surface in 3d in python (matplotlib)?

I'm able to plot a surface in 3d in matplotlib, but I also need to plot a line, and a point on the surface. The surface that the line are fine, but the point does not show up on the surface for some reason, though. Here is the code:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-2.0, 2.0, 0.05)
Y = np.arange(-1.0, 3.0, 0.05)
X, Y = np.meshgrid(X, Y)
Z = (np.ones([np.shape(X)[0],np.shape(X)[1]])-X)**2+100*(Y-(X)**2)**2
Gx, Gy = np.gradient(Z) # gradients with respect to x and y
G = (Gx**2+Gy**2)**.5 # gradient magnitude
N = G/G.max() # normalize 0..1
surf = ax.plot_surface(
X, Y, Z, rstride=1, cstride=1,
facecolors=cm.jet(N),
linewidth=0,
antialiased=False,
shade=False)
plt.hold(True)
ax.hold(True)
# add the unit circle
x_1 = np.arange(-1.0, 1.0, 0.005)
x_2 = np.arange(-1.0, 1.0, 0.005)
y_1 = np.sqrt(np.ones(len(x_1)) - x_1**2)
y_2 = -np.sqrt(np.ones(len(x_2)) - x_2**2)
x = np.array(x_1.tolist() + x_2.tolist())
y = np.array(y_1.tolist() + y_2.tolist())
z = (np.ones(len(x))-x)**2+100*(y-(x)**2)**2
ax.plot(x, y, z, '-k')
plt.hold(True)
ax.hold(True)
ax.scatter(np.array([0.8]),
np.array([0.6]),
np.array([0.045]),
color='red',
s=40
)
# Get current rotation angle
print 'rotation angle is ', ax.azim
# Set rotation angle to 60 degrees
ax.view_init(azim=60)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
The issue is that the point does not show up on the surface. Now, when I replace this code:
ax.scatter(np.array([0.8]),
np.array([0.6]),
np.array([0.045]),
color='red',
s=40
)
...with this code (i.e. just adding to the last value)...
ax.scatter(np.array([0.8]),
np.array([0.6]),
np.array([0.045+800]),
color='red',
s=40
)
...then it shows up. But I can't think of a reason why it is not showing up when I want to plot the actual value in the surface. Does someone know how to fix this?
(As an aside, I'd love to get rid of the weird line in the middle of the unit circle that I plot on the surface. I can't seem to get rid of it.)
Much obliged!

Fixing jagged edges of 3D plot, selecting an appropriate mask

So I have some 3D data that I am able to plot just fine except the edges look jagged.
The relevant code:
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(-1, 1, 0.01)
y = np.arange(-1, 1, 0.01)
x, y = np.meshgrid(x, y)
rho = np.sqrt(x**2 + y**2)
# Attempts at masking shown here
# My Mask
row=0
while row<np.shape(x)[0]:
col=0
while col<np.shape(x)[1]:
if rho[row][col] > 1:
rho[row][col] = None
col=col+1
row=row+1
# Calculate & Plot
z = rho**2
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, rstride=8, cstride=8, cmap=cm.bone, alpha=0.15, linewidth=0.25)
plt.show()
Produces:
This is so close to what I want except the edges are jagged.
If I disable my mask in the code above & replace it with rho = np.ma.masked_where(rho > 1, rho) it gives:
It isn't jagged but not want I want in the corners.
Any suggestions on different masking or plotting methods to get rid of this jaggedness?
Did you consider using polar coordinates (like in this example) ?
Something like:
import numpy as np
from matplotlib import cm
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# create supporting points in polar coordinates
r = np.linspace(0,1.25,50)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
x, y = R * np.cos(P), R * np.sin(P)
rho = np.sqrt(x**2 + y**2)
# Calculate & Plot
z = rho**2
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1, cmap=cm.bone, alpha=0.15, linewidth=0.25)
plt.show()

Categories