简体   繁体   English

绘制二维向量(二维):如何在 Python 中的球坐标中 plot 向量场

[英]Plotting Vectors 2D (Two-Dimensional): How to plot a vector field in Spherical Coordinates in Python

How to plot a vector field in Spherical Coordinates in Python like this equation:如何在 Python 的球坐标中 plot 一个矢量场,如下式:

Is it possible to plot a graph (2D or 3D) of vector field是否可以 plot 绘制矢量场的图形(2D 或 3D) in spherical coordinates using matplotlib?在球坐标中使用 matplotlib?

Need some help.需要一些帮助。

I have ignored the normalisation by r^3 in H .我忽略了Hr^3的归一化。

import numpy as np
import matplotlib.pyplot as plt

pts = 100
r, theta = np.linspace(-1, 1, pts), np.linspace(0, 2*np.pi, pts)

r_comp = 2*np.cos(theta)
theta_comp = np.sin(theta)


x_comp = r_comp * np.sin(theta_comp)
y_comp = r_comp * np.cos(theta_comp)

plt.figure()
plt.quiver(x_comp, y_comp, r_comp, theta_comp)

在此处输入图像描述

Since you posted this on Physics.SE , I assume your equation is using the physics convention for spherical coordinates , with theta=0 at the north pole.由于您在 Physics.SE 上发布了此内容,因此我假设您的方程式使用球坐标的物理约定,北极处的 theta=0。

You can easily plot 3D vector fields using SageMath , which is a huge mathematics / algebra system built on top of Python.您可以使用SageMath轻松plot 3D 矢量场,这是一个建立在 Python 之上的庞大数学/代数系统。 If you can write Python, it's easy to start working with SageMath.如果你能写出 Python,就很容易开始使用 SageMath。 You don't even need to install it: you can run Sage scripts in your Web browser, on the SageMathCell server.您甚至不需要安装它:您可以在SageMathCell服务器上的 Web 浏览器中运行 Sage 脚本。

Sage has sophisticated support for vector fields , with a variety of coordinate systems built-in. Sage 对矢量场具有复杂的支持,并内置了各种坐标系。 It can do Cartesian plots working in spherical coordinates , but it's allegedly rather slow, and I haven't experimented with it.可以在球坐标中进行笛卡尔绘图,但据说速度相当慢,而且我还没有尝试过。

The code below creates an interactive 3D plot of your vector field, doing the coordinate transformations explicitly.下面的代码创建一个交互式 3D plot 您的矢量场,明确地进行坐标转换。 Sage lets us define symbolic variables, which can be used to create equations and do algebra and calculus. Sage 让我们定义符号变量,这些变量可用于创建方程并进行代数和微积分。 Sage syntax is almost identical to plain Python, although as a mathematical convenience it permits the use of ^ as well as ** for exponentiation (Sage uses ^^ for exclusive-or). Sage 语法几乎与普通的 Python 相同,尽管为了数学上的方便,它允许使用^**进行求幂(Sage 使用^^进行异或)。

The Sage plot_vector_field3d function works with Cartesian coordinates. Sage plot_vector_field3d function 使用笛卡尔坐标。 It expects its function argument to be a list, tuple, or vector of functions that each take x, y, z args.它期望其 function 参数是一个列表、元组或函数向量,每个函数都采用 x、y、z 参数。 So we need to transform those args to polar form, call the H function with the polar args, then transform the polar value returned by H to Cartesian.所以我们需要将这些args转换为极坐标形式,用极坐标args调用H function,然后将H返回的极坐标值转换为笛卡尔坐标。 By working with symbolic variables and functions, we can transform the H function itself, as this code illustrates.通过使用符号变量和函数,我们可以转换H function 本身,如这段代码所示。

# Create some symbolic variables
x, y, z = var('x,y,z')
rad, th, phi = var('rad,th,ph')

# Transform polar coords to Cartesian
def sphere_xyz(R, theta, phi):
    r = R * sin(theta)
    return r * cos(phi), r * sin(phi), R * cos(theta)

# Transform Cartesian coords to polar
def xyz_sphere(X, Y, Z):
    r2 = X^2 + Y^2
    r = sqrt(r2)
    R = sqrt(r2 + Z^2)
    return R, atan2(r, Z), atan2(Y, X)

# The equation in polar coords
H_polar(rad, th, ph) = (2 * cos(th) / rad^3, sin(th) / rad^3, 0)

# Transform equation to Cartesian
R, theta, phi = xyz_sphere(x, y, z)
H_xyz(x, y, z) = sphere_xyz(*H_polar(rad=R, th=theta, ph=phi))
print(H_xyz)

lo, hi = 3, 6
P = plot_vector_field3d(H_xyz,
  (x, lo, hi), (y, lo, hi), (z, lo, hi),
  colors='rainbow', plot_points=[6, 6, 6])

perspective = False
projection = "perspective" if perspective else "orthographic"
P.show(frame=True, projection=projection)

The script prints this for the transformed version of the H equation.该脚本为H方程的转换版本打印此内容。

(x, y, z) |--> (2*z*sin(sqrt(x^2 + y^2)/(x^2 + y^2 + z^2)^2)/(x^2 + y^2 + z^2)^2, 0, 2*z*cos(sqrt(x^2 + y^2)/(x^2 + y^2 + z^2)^2)/(x^2 + y^2 + z^2)^2)

Here's a screenshot of the plot, using an orthographic projection.这是 plot 的屏幕截图,使用正交投影。

3D 矢量场 It's a lot easier to see the structure in the interactive 3D view .交互式 3D 视图中查看结构要容易得多。

You can drag the view with the mouse & zoom with the mouse scroll wheel, the shift & ctrl keys can also be used.您可以使用鼠标拖动视图并使用鼠标滚轮缩放,也可以使用 shift 和 ctrl 键。 On touchscreens, use one finger to rotate, two fingers to pan & zoom.在触摸屏上,用一根手指旋转,用两根手指平移和缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM