如何在Python中使用quiver for Polar? [英] How do I use quiver in Python for polar?

查看:67
本文介绍了如何在Python中使用quiver for Polar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,是的,我已经阅读了有关此问题的先前线程和文档,例如 这是一条会聚的运河,它显示了速度/矢量场.显然,我只有一个径向分量,但它随角度 theta 变化.当我们朝(alpha)方向向下(流)时,这种箭头模式会重复.因此,绘制应该很简单,对.这是径向速度分量的方程:

Firstly, yes I have read previous threads and documentation about this issue, for example This is a converging canal, it shows a velocity/vector field. Clearly I only have a radial component, but it changes with the angle theta. This pattern of arrows repeats itself as we go down(stream) towards alpha. So it should be simple to plot, right. Here is the equation for the radial velocity component:

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

Now, before I show my code, I have stored values of f(theta) for a number of thetas. This function, f, has to be numerically solved and I have stored it as a vector, u[0]. This what I do in my code as of now:

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的第五个表达式是一次绝望的尝试,即将网状网格中固定长度的所有r乘以u [0],但它们的维数不相同,因此不起作用.所以我被卡住了.

The fifth expression for the variable dr was a desperate attempt at multiplying all r of a fix length in the meshgrid with u[0], but these do not have the same dimensions, therefore it doesn't work. So I am stuck.

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

My question is how I obtain a vectorfield for the converging canal? I can't really put the last pieces togetether, do I manipulate the meshgrid?

到目前为止在 MATLAB 中的结果:

Results so far in MATLAB:

编辑上面的代码来自我文本开头的链接.我对 dr 和 dt 进行了一些更改,但除此之外什么都没有.

Edit The code above was taken from the link in the beginning of my text. I made some changes to dr and dt, but otherwise nothing.

推荐答案

您的代码唯一真正的问题是 numpy 问题,即您的 dr 中有错误方面.稍微调整一下代码:

The only real problem with your code was a numpy problem, i.e. in your dr has the wrong dimensions. With slight adjustments to your code:

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,否则箭头会变得太长以至于它们指向原点的另一侧,这看起来很奇怪.

Note that in the radii definition, I moved the lower limit from 0.1 to 0.2 as otherwise the arrows get so long that they point to the other side of the origin, which looks quite weird.

这篇关于如何在Python中使用quiver for Polar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆