【问题标题】:magnetic field visualization with quiver-function in PythonPython中带有颤动功能的磁场可视化
【发布时间】:2011-02-19 12:04:17
【问题描述】:

我想用 quiver 功能可视化电线的磁场。

#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:我想使用 quiver,但它不起作用。

【问题讨论】:

  • 问题是什么已经不清楚了。你能澄清一下吗?
  • 我想使用 quiver,但它不起作用。

标签: python numpy matplotlib


【解决方案1】:

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

【讨论】:

  • 不,我想使用 2d。 U 包含磁场矢量的 x 分量。 V 包含磁场矢量的 y 分量。
  • 在您的示例中,U 和 V 的形状应为 (5,5)(以匹配 X 和 Y 的暗度。
  • 另外,如果您不希望 U 和 V 相同,bfield 也应该返回 b 的副本
  • Paul:我现在用 xwidth 做到了。 - U 和 V 现在不同了。
【解决方案2】:
ValueError: too many values to unpack

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

【讨论】:

  • 你能再解释一下吗?在哪一行?这里? --> U = bfield(X,Y,z,0)???
  • 文件“D:\Python26\Lib\site-packages\matplotlib\quiver.py”,第 343 行,在 _parse_args nr,nc = U.shape
  • @kame - 如果还有问题,请说明问题所在。
  • @kame:有什么不好的?也就是说,quiver 有效,但我认为您的意思是您没有摆脱它想要的东西。要弄清楚这一点,我们需要知道您想要什么,并将其与您得到的进行比较。仅根据代码很难确定您希望它做什么与它实际做什么,所以如果您用文字准确地说出您想要什么,我们也许可以提供帮助。
猜你喜欢
  • 2023-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多