Python Matplotlib-如何在极坐标中绘制流线?
我一直试图在matplotlib 1.4.3中绘制极轴上的流线。streamplot功能自1.2.0开始就存在,文档认为它是功能性的和稳定的。下面是一个小测试脚本:Python Matplotlib-如何在极坐标中绘制流线?,python,matplotlib,polar-coordinates,Python,Matplotlib,Polar Coordinates,我一直试图在matplotlib 1.4.3中绘制极轴上的流线。streamplot功能自1.2.0开始就存在,文档认为它是功能性的和稳定的。下面是一个小测试脚本: from matplotlib import pyplot as plt import numpy as np # Define polar grid r = np.arange(0,2001,50) theta = np.arange(-np.pi, np.pi+np.pi/180, 2*np.pi/180) r2D, thet
from matplotlib import pyplot as plt
import numpy as np
# Define polar grid
r = np.arange(0,2001,50)
theta = np.arange(-np.pi, np.pi+np.pi/180, 2*np.pi/180)
r2D, theta2D = np.meshgrid(r, theta)
# Define some data
u = -np.sin(theta2D)
v = np.cos(theta2D)
# Set up axes
fig = plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)
# Plot streamlines
ax.streamplot(r, theta, u, v, color='k', density=1, linewidth=1)
此脚本因以下回溯而失败:
Traceback (most recent call last):
File "streamline_test.py", line 15, in <module>
ax.streamplot(r, theta, u, v, color='k', density=1, linewidth=1)
File "python2.7/site-packages/matplotlib/axes/_axes.py", line 4204, in streamplot
zorder=zorder)
File "python2.7/site-packages/matplotlib/streamplot.py", line 167, in streamplot
axes.add_patch(p)
File "python2.7/site-packages/matplotlib/axes/_base.py", line 1568, in add_patch
self._update_patch_limits(p)
File "python2.7/site-packages/matplotlib/axes/_base.py", line 1586, in _update_patch_limits
vertices = patch.get_path().vertices
File "python2.7/site-packages/matplotlib/patches.py", line 4033, in get_path
_path, fillable = self.get_path_in_displaycoord()
File "python2.7/site-packages/matplotlib/patches.py", line 4054, in get_path_in_displaycoord
shrinkB=self.shrinkB * dpi_cor
File "python2.7/site-packages/matplotlib/patches.py", line 2613, in __call__
shrinked_path = self._shrink(clipped_path, shrinkA, shrinkB)
File "python2.7/site-packages/matplotlib/patches.py", line 2586, in _shrink
left, right = split_path_inout(path, insideA)
File "python2.7/site-packages/matplotlib/bezier.py", line 246, in split_path_inout
ctl_points, command = next(path_iter)
StopIteration
回溯(最近一次呼叫最后一次):
文件“streamlined_test.py”,第15行,在
最大流图(r,θ,u,v,color='k',density=1,linewidth=1)
streamplot中第4204行的文件“python2.7/site packages/matplotlib/axes/_axes.py”
zorder=zorder)
streamplot中第167行的文件“python2.7/site packages/matplotlib/streamplot.py”
轴。添加补片(p)
文件“python2.7/site packages/matplotlib/axes/_base.py”,第1568行,在add_补丁中
自我更新补丁限制(p)
文件“python2.7/site packages/matplotlib/axes/_base.py”,第1586行,在更新补丁中
顶点=面片。获取路径()。顶点
文件“python2.7/site packages/matplotlib/patches.py”,第4033行,在get_路径中
_path,fillable=self.get\u path\u in\u displaycoord()
文件“python2.7/site packages/matplotlib/patches.py”,第4054行,在get_路径中
收缩b=自收缩b*dpi\u cor
文件“python2.7/site packages/matplotlib/patches.py”,第2613行,在调用中__
收缩路径=自身。\u收缩(剪裁路径、收缩A、收缩B)
文件“python2.7/site packages/matplotlib/patches.py”,第2586行,在
左,右=拆分路径输入(路径,内部)
文件“python2.7/site packages/matplotlib/bezier.py”,第246行,在split_path_inout中
控制点,命令=下一步(路径)
停止迭代
显然,streamplot一直在迭代,必须在某个点停止。我还尝试了将一组规则间隔的笛卡尔点应用于极轴,但同样失败。使用笛卡尔坐标轴绘制极坐标图不是一个选项,因为我需要一个极坐标网格,但这样的网格在笛卡尔坐标系中不是规则间隔的,streamplot需要规则间隔的点
有人知道如何让matplotlib以极坐标绘制流线吗?您只需切换径向坐标和方位坐标即可。考虑下面的图,以及生成它的代码。请注意第三个参数中的半径除法是
streamplot()
,它将线速度转换为角速度:
导入数学
进口numpy
导入matplotlib
从matplotlib导入pyplot
pyplot.gcf().add_轴([0.1,0.1,0.8,0.8],polar=True)
#坐标
r=numpy.linspace(0,1,11)
p=numpy.linspace(-math.pi,math.pi,361)
rg,pg=numpy.meshgrid(r,p)
def重复(x):
返回numpy.full_-like(r,x)
ε=1e-8
#水平单位矢量的圆柱分量
xr=numpy.cos(pg)
xp=-numpy.sin(pg)
#垂直单位矢量的圆柱分量
yr=numpy.sin(pg)
yp=单位成本(pg)
#流线的起点
sx=numpy.transpose([
numpy.hstack([repeat(-math.pi/2),repeat(math.pi/2)],
numpy.hstack([r,r])
])
sy=numpy.transpose([
numpy.hstack([repeat(-math.pi+epsilon),repeat(0),repeat(math.pi epsilon)],
numpy.hstack([r,r,r])
])
#流线
pyplot.streamplot(
pg.transpose(),rg.transpose(),(xp/rg).transpose(),xr.transpose(),
颜色=红色,起始点=sx)
pyplot.streamplot(
pg.transpose(),rg.transpose(),(yp/rg).transpose(),yr.transpose(),
颜色=蓝色,起始点=sy)
pyplot.ylim(0,1)
pyplot.annotate(
matplotlib.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
(0,0),(1,0),“轴分数”,“轴分数”,
ha='left',va='top')
pyplot.savefig('stream.png'))