magnetic field visualization with quiver-function in Python
I want to visualize the magnetic field of a wire with the quiver function.
#Calculation of a magnetic field of a wire
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
I = 100000000000
constant = 1e-7/(4*np.pi)
# wire elements; always lenght one
coord = [(10,10,0), (11,10,0), (12,10,0), (13,10,0), (14,10,0), (15,10,0), (16,10,0), (17,10,0), (18,10,0),
(19,10,0), (19,11,0), (19,12,0), (19,13,0)]
xwidth = 3
ywidth = 3
z = 1
b = np.zeros((xwidth,ywidth))
# calculate the b-field
def bfield(x,y,z,c):
for x in range(xwidth):
for y in range(ywidth):
# number of wire elements
for i in range(1,12):
rx = x-(coord[i][0]+coord[i+1][0])/2.
ry = y-(coord[i][1]+coord[i+1][1])/2.
rz = z * 1.0 # = z-0
r = (rx**2+ry**2+rz**2)**0.5 # distance r between field and middle of the wire
dl = np.array([(coord[i+1][0]-coord[i][0]), (coord[i+1][1]-coord[i][1]), 0])
bb = np.cross(dl, np.array([rx,ry,rz]))
开发者_JAVA技巧 e = constant*I*bb/r**3
print e
#print e[0], e[1]
b[x,y] += e[c] # EDIT
return b
X,Y = meshgrid(arange(0,xwidth,1),arange(0,ywidth,1))
U = bfield(X,Y,z,0)
V = bfield(X,Y,z,1)
quiver(X,Y,U,V)
xlim(0,xwidth)
ylim(0,ywidth)
show()
EDIT 2: How can I plot lines of the coords in the plot? EDIT 3: I want to use quiver, but it doesn't work.
it looks like quiver only supports 2d plots right now, but you could make it 3d by plotting multiple 2d layers into a 3d plot. You can follow my example to see how to do these layers.
ValueError: too many values to unpack
That means you have more values on the RHS than variables on the LHS
精彩评论