【发布时间】:2016-10-08 20:51:03
【问题描述】:
我需要绘制在 (r,theta) 坐标中的不均匀网格上定义的标量和矢量场的等高线和颤动图。
作为我遇到的问题的一个最小示例,考虑磁偶极子的 Stream function 的等高线图,这种函数的等高线是相应矢量场的流线(在这种情况下,磁场)。
以下代码采用 (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()
如果我在轮廓函数调用中交换 x 和 y,我会得到想要的结果:
当我尝试制作在同一网格上定义并映射到 x-y 平面的 矢量场 的颤动图时,也会发生同样的事情,只是在函数调用中交换 x 和 y 不再起作用.
好像我在某个地方犯了一个愚蠢的错误,但我不知道是什么。
【问题讨论】:
标签: python numpy matplotlib contour polar-coordinates