Matplotlib - 投影极坐标中的轮廓和箭头图

时间:2016-10-08 20:51:04

标签: python numpy matplotlib contour polar-coordinates

我需要绘制在(r,theta)坐标中不均匀网格上定义的标量和矢量场的轮廓和箭头图。

作为我遇到的问题的最小例子,考虑磁偶极子的 Stream function 的等高线图,这种函数的轮廓是相应矢量场的流线(in这种情况下,磁场)。

下面的代码在(r,theta)坐标中采用不均匀的网格,将其映射到笛卡尔平面并绘制流函数的等高线图。

import numpy as np
import matplotlib.pyplot as plt

r = np.logspace(0,1,200)
theta = np.linspace(0,np.pi/2,100)

N_r = len(r)
N_theta = len(theta)

# Polar to cartesian coordinates
theta_matrix, r_matrix = np.meshgrid(theta, r)
x = r_matrix * np.cos(theta_matrix)
y = r_matrix * np.sin(theta_matrix)

m = 5
psi = np.zeros((N_r, N_theta))

# Stream function for a magnetic dipole
psi = m * np.sin(theta_matrix)**2 / r_matrix

contour_levels = m * np.sin(np.linspace(0, np.pi/2,40))**2.

fig, ax = plt.subplots()
# ax.plot(x,y,'b.')  # plot grid points
ax.set_aspect('equal')
ax.contour(x, y, psi, 100, colors='black',levels=contour_levels)
plt.show()

出于某种原因,我得到的情节看起来并不正确: Contour plot of a stream function for a magnetic dipole.

如果我在轮廓函数调用中交换x和y,我会得到所需的结果: enter image description here

当我尝试在同一网格上定义矢量场的箭头图并映射到xy平面时,会发生同样的事情,除了在函数调用中交换x和y不再有效。

似乎我在某个地方犯了一个愚蠢的错误,但我无法弄清楚它是什么。

1 个答案:

答案 0 :(得分:1)

如果psi = m * np.sin(theta_matrix)**2 / r_matrix 然后psi随着θ从0变为pi / 2而增加,psi随r增加而减小。

因此,当θ增加时,psi的轮廓线应随r增加。结果 在从中心向外辐射的逆时针方向的曲线中。这是 与您发布的第一个图一致,以及您的代码的第一个版本返回的结果

ax.contour(x, y, psi, 100, colors='black',levels=contour_levels)

确认结果合理性的另一种方法是查看psi的表面图:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d

r = np.logspace(0,1,200)
theta = np.linspace(0,np.pi/2,100)

N_r = len(r)
N_theta = len(theta)

# Polar to cartesian coordinates
theta_matrix, r_matrix = np.meshgrid(theta, r)
x = r_matrix * np.cos(theta_matrix)
y = r_matrix * np.sin(theta_matrix)

m = 5

# Stream function for a magnetic dipole
psi = m * np.sin(theta_matrix)**2 / r_matrix

contour_levels = m * np.sin(np.linspace(0, np.pi/2,40))**2.

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_aspect('equal')

ax.plot_surface(x, y, psi, rstride=8, cstride=8, alpha=0.3)
ax.contour(x, y, psi, colors='black',levels=contour_levels)
plt.show()

enter image description here

相关问题