我如何在 Python 中使用 quiver for polar?

How do I use quiver in Python for polar?

首先,是的,我已经阅读了有关此问题的先前主题和文档,例如 How to make a quiver plot in polar coordinates. This didn't help me all the way. Let me show you what I am working with and then some code. 这是一条会聚的运河,它显示了一个 velocity/vector 字段。显然我只有一个径向分量,但它随角度 theta 的变化而变化。当我们向下(流)向 alpha 时,这种箭头模式会重复出现。所以绘制应该很简单,对吧。这是径向速度分量的方程式:

现在,在我展示我的代码之前,我已经存储了多个 theta 的 f(theta) 值。这个函数 f 必须进行数值求解,我将其存储为向量 u[0]。这是我现在在我的代码中所做的:

radii = np.linspace(0.1,1,11)
thetas = np.linspace(-alpha,alpha,20)
theta, r = np.meshgrid(thetas, radii)

q = 0.0001


dr = [-q/x for x in radii]*u_sol[0]
dt = 0

f = plt.figure()
ax = f.add_subplot(111, polar=True)

ax.quiver(theta, r, dr * cos(theta) - dt * sin (theta), dr * sin(theta) +     
dt* cos(theta))

变量 dr 的第五个表达式是拼命尝试将 meshgrid 中固定长度的所有 r 与 u[0] 相乘,但它们没有相同的维度,因此它不起作用。所以我卡住了。

我的问题是如何获得会聚运河的矢量场?我真的不能把最后的部分放在一起,我要操纵网格吗?

目前在 MATLAB 中的结果:

编辑 上面的代码摘自我文章开头的link。我对 dr 和 dt 做了一些更改,但除此之外什么都没有。

您的代码唯一真正的问题是 numpy 问题,即您的 dr 尺寸错误。对您的代码稍作调整:

from matplotlib import pyplot as plt
import numpy as np

#to make the code runnable
u_sol = [1]
alpha0 = 5*np.pi/180
alpha = 10*np.pi/180

radii = np.linspace(0.2,1,10)
print(radii)
thetas = np.linspace(alpha0-alpha,alpha0+alpha,20)
print(thetas)
theta, r = np.meshgrid(thetas, radii)

q = 0.0001


#dr = [-q/x for x in radii]*u_sol[0]
dr = -q/r
dt = 0

f = plt.figure()
ax = f.add_subplot(111, polar=True)

ax.quiver(
    theta, r,
    dr * np.cos(theta) - dt * np.sin(theta),
    dr * np.sin(theta) + dt * np.cos(theta),
)

plt.show()

我得到以下图像:

请注意,在 radii 定义中,我将下限从 0.1 移到了 0.2,否则箭头会变得太长以至于指向原点的另一侧,这看起来很奇怪。