Plotting 3D Vector Field - python

I am trying to plot a 3D vector field. I used the following example as guidance:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.8))
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
ax.quiver(x, y, z, u, v, w, length=0.1)
plt.show()
This example comes from the matplotlib examples library. However, I wanted to try a different function to replace u, v and w like this:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
x,y,z = np.meshgrid(np.arange(-0.1,0.005,0.1),np.arange(-0.1,0.005,0.1),np.arange(-0.1,0.005,0.1))
VolMag = 3.218E-6 #Volume of magnet in experiment in m^3
BR = np.sqrt(x**2 + y**2 + z**2)
MagMoment = np.array([0,0,(BInput*VolMag)/u0])
Bx = (u0/(4*np.pi))*(3*x*MagMoment*z)/BR**5
By = (u0/(4*np.pi))*(3*y*MagMoment*z)/BR**5
Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3)
ax.quiver(x,y,z,Bx,By,Bz,length=0.1)
plt.show()
This is giving me an "invalid syntax" error for line 16. Why is this? I only changed the function and some of the names of the definitions.

It looks like your missing a closing parenthesis on this line.
Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3)
should be
Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3))

Related

Correctly interpolate 3D vector field with python scipy

GOAL
My goal is to interpolate a 3D vector field using python.
CODE
Original Vector field
import numpy as np
import matplotlib.pyplot as plt
# For interpolation
from scipy.interpolate import RegularGridInterpolator
#%% VECTOR FIELD
xx, yy, zz = np.meshgrid(np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.8))
uu = np.sin(np.pi * xx) * np.cos(np.pi * yy) * np.cos(np.pi * zz)
vv = -np.cos(np.pi * xx) * np.sin(np.pi * yy) * np.cos(np.pi * zz)
ww = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * xx) * np.cos(np.pi * yy) *
np.sin(np.pi * zz))
# Ravel -> make 1D lists
x = np.ravel(xx)
y = np.ravel(yy)
z = np.ravel(zz)
u = np.ravel(uu)
v = np.ravel(vv)
w = np.ravel(ww)
Interpolation Function (something is wrong here)
#%% INTERPOLATION FUNCTION
def interpolate_field(x,y,z,u,v,w,new_points):
x = np.unique(x)
y = np.unique(y)
z = np.unique(z)
u = np.reshape(u, (len(x), len(y), len(z)))
v = np.reshape(u, (len(x), len(y), len(z)))
w = np.reshape(u, (len(x), len(y), len(z)))
u_int_f = RegularGridInterpolator((x, y, z), u)
v_int_f = RegularGridInterpolator((x, y, z), v)
w_int_f = RegularGridInterpolator((x, y, z), w)
u_int = u_int_f(new_points)
v_int = v_int_f(new_points)
w_int = w_int_f(new_points)
return u_int, v_int, w_int
Evaluate interpolation at new points
#%% EVALUATE INTERPOLATION FUNCTION
new_grid = np.meshgrid(
np.linspace(np.min(x), np.max(x), 20),
np.linspace(np.min(y), np.max(y), 20),
np.linspace(np.min(z), np.max(z), 3)
, indexing="xy")
# create list of new_points
new_points = np.vstack(list(map(np.ravel, new_grid))).T
# get vector field values at new points
uint, vint, wint = interpolate_field(x,y,z,u,v,w,new_points)
new_points = np.array(new_points)
xn = new_points[:,0]
yn = new_points[:,1]
zn = new_points[:,2]
# PLOT
fig = plt.figure(dpi=300)
ax = fig.gca(projection='3d')
ax.quiver(xn, yn, zn, uint, vint, wint, length=0.1)
plt.show()
THE ISSUE
As you can see something is wrong with the interpolation function since the resulting vector plot does not show the same behavior as the original at all.
Here is a way to do it using RegularGridInterpolator:
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import RegularGridInterpolator
def interp_field(field, coords, shape):
interpolator = RegularGridInterpolator(
(gridZ[:, 0, 0], gridY[0, :, 0], gridX[0, 0, :]), field)
return interpolator(coords).reshape(shape)
gridZ, gridY, gridX = np.mgrid[-0.8:1:3j, -0.8:1:10j, -0.8:1:10j]
u = np.sin(np.pi * gridX) * np.cos(np.pi * gridY) * np.cos(np.pi * gridZ)
v = -np.cos(np.pi * gridX) * np.sin(np.pi * gridY) * np.cos(np.pi * gridZ)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * gridX) * np.cos(np.pi * gridY) *
np.sin(np.pi * gridZ))
interp_gridZ, interp_gridY, interp_gridX = np.mgrid[
-0.8:1:3j, -0.8:1:20j, -0.8:1:20j]
interp_coords = np.column_stack(
(interp_gridZ.flatten(), interp_gridY.flatten(), interp_gridX.flatten()))
interp_u = interp_field(u, interp_coords, interp_gridX.shape)
interp_v = interp_field(v, interp_coords, interp_gridX.shape)
interp_w = interp_field(w, interp_coords, interp_gridX.shape)
fig, (ax, bx) = plt.subplots(ncols=2, subplot_kw=dict(projection='3d'),
constrained_layout=True)
ax.quiver(gridX, gridY, gridZ, u, v, w, length=0.3)
bx.quiver(interp_gridX, interp_gridY, interp_gridZ,
interp_u, interp_v, interp_w, length=0.3)
plt.show()
The resulting plot looks like that:

Plotting 3D vectors using matplotlib

I followed this thread in order to implement plotting 3D vectors.
When running the code with the given examples of vectors it seems fine, but when I try to use my own vectors I'm getting vectors without direction:
I'm trying to understand what is wrong with my data.
def plot_vectors(vectors):
fig = plt.figure()
# ax = fig.gca(projection='3d')
# ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')
for vector in vectors:
# v1 = np.array([vector[0], vector[1], vector[2]])
v_length = np.linalg.norm(vector)
x = vector[0]
y = vector[1]
z = vector[2]
# Make the direction data for the arrows
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z))
ax.quiver(x, y, z, u, v, w, pivot='tail', length=v_length, arrow_length_ratio=0.3 / v_length)
# ax.quiver(vector[0], vector[1], vector[2], vector[3], vector[4], vector[5],
# pivot='tail', length=vlength, arrow_length_ratio=0.3 / v_length)
# ax.set_xlim([-4, 4])
# ax.set_ylim([-4, 4])
# ax.set_zlim([0, 4])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
run it with these vectors for example:
plot_vectors([[95.38065011522669, -15.867331426507993, 267.4801091009956],
[-52.379559088282384, -1.0021180591632532, 163.80875985057938]])
I ran your code and looks like using .3 / v_length for the arrow_length_ratio yields a super tiny arrow head for your values of x, y, and z. I would use a different calculation here...
perhaps something like .001 * v_length will work in this case.
I would play around with it until you find something that you like and that works for all your data!

Python plotting trigonometrical func

I have a function 2*x*arcctg(x) - 1, and i try to plot it in Python:
import os
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi / 2)
y = 2 * x * np.cos(2 * x) / np.sin(2 * x)
plt.plot(x, y)
plt.axis('tight')
plt.show()
but it's plot smthg like that:
and when i plot it in wolfram it looks:
What am i doing wrong?
The function should be:
2*x*arcctg(x) - 1
But arcctg(x) is not cos(2x)/sin(2x) (the expression you describe in your code). A ctg is the co-tangens, so cos(x)/sin(x). So that means that arcctg(x) is arctan(1/x).
So you can use:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, np.pi / 2)
y = 2 * x * np.arctan(1/x) - 1
plt.plot(x, y)
plt.axis('tight')
plt.show()
This produces the following plot:
Which matches with the plot in the question.
In case you want to make the plot look more than the one in Wolfram Alpha, you can like #MSeifert says, set the range from -pi/2 to pi/2, like:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-np.pi / 2, np.pi / 2, 1000)
y = 2 * x * np.arctan(1/x) - 1
plt.plot(x, y)
plt.axis('tight')
plt.show()
this then produces:

I am getting the 'too many values to unpack' error on 3Dquiver. Any idea how I can fix this?

I'm just trying to compile this example, and I get the 'too many values to unpack' error
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.2),
np.arange(-0.8, 1, 0.8))
u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
np.sin(np.pi * z))
ax.quiver(x, y, z, u, v, w, length=0.1)
plt.show()
Here's tell me that's the error that I get
in <module>()
22 w=z*((y - b)/d_1 - (y - b)/d_2 + (y + b)/d_3 - (y + b)/d_4)
23
---> 24 ax.quiver(x, y, z, u, v, w, length=0.1)
25
26 plt.show()
You need at least version 1.4.x of matplotlib for 3D quiver plots.
I ran into this problem on Linux Mint 17.3 which only has 1.3.1. The way I upgraded was to uninstall python-matplotlib, install python-pip, and install matplotlib (version 1.5.1) via:
sudo pip install matplotlib

Matplotlib plot pulse propagation in 3d

I'd like to plot pulse propagation in such a way at each step, it plots the pulse shape. In other words, I want a serie of x-z plots, for each values of y. Something like this (without color):
How can I do this using matplotlib (or Mayavi)? Here is what I did so far:
def drawPropagation(beta2, C, z):
""" beta2 in ps / km
C is chirp
z is an array of z positions """
T = numpy.linspace(-10, 10, 100)
sx = T.size
sy = z.size
T = numpy.tile(T, (sy, 1))
z = numpy.tile(z, (sx, 1)).T
U = 1 / numpy.sqrt(1 - 1j*beta2*z * (1 + 1j * C)) * numpy.exp(- 0.5 * (1 + 1j * C) * T * T / (1 - 1j*beta2*z*(1 + 1j*C)))
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
surf = ax.plot_wireframe(T, z, abs(U))
Change to:
ax.plot_wireframe(T, z, abs(U), cstride=1000)
and call:
drawPropagation(1.0, 1.0, numpy.linspace(-2, 2, 10))
will create the following graph:
If you need the curve been filled with white color:
import numpy
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from matplotlib.collections import PolyCollection
def drawPropagation(beta2, C, z):
""" beta2 in ps / km
C is chirp
z is an array of z positions """
T = numpy.linspace(-10, 10, 100)
sx = T.size
sy = z.size
T = numpy.tile(T, (sy, 1))
z = numpy.tile(z, (sx, 1)).T
U = 1 / numpy.sqrt(1 - 1j*beta2*z * (1 + 1j * C)) * numpy.exp(- 0.5 * (1 + 1j * C) * T * T / (1 - 1j*beta2*z*(1 + 1j*C)))
fig = pyplot.figure()
ax = fig.add_subplot(1,1,1, projection='3d')
U = numpy.abs(U)
verts = []
for i in xrange(T.shape[0]):
verts.append(zip(T[i, :], U[i, :]))
poly = PolyCollection(verts, facecolors=(1,1,1,1), edgecolors=(0,0,1,1))
ax.add_collection3d(poly, zs=z[:, 0], zdir='y')
ax.set_xlim3d(numpy.min(T), numpy.max(T))
ax.set_ylim3d(numpy.min(z), numpy.max(z))
ax.set_zlim3d(numpy.min(U), numpy.max(U))
drawPropagation(1.0, 1.0, numpy.linspace(-2, 2, 10))
pyplot.show()

Categories