用Python中的箭头函数进行磁场可视化

时间:2011-02-19 12:04:17

标签: python numpy matplotlib

我想用箭袋功能可视化导线的磁场。

#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]))
                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()

编辑2:我如何在情节中绘制坐标线? 编辑3:我想使用箭袋,但它不起作用。

2 个答案:

答案 0 :(得分:1)

看起来quiver现在只支持2d图,但你可以通过将多个2d图绘制成3d图来使其成为3d。您可以按照我的example查看如何执行这些图层。

答案 1 :(得分:0)

ValueError: too many values to unpack

这意味着您在RHS上的值比LHS上的变量更多