绘制改变坐标的streamplot

时间:2016-03-14 14:41:08

标签: python-3.x matplotlib

我想绘制一个以极坐标给出的字段。这可以用箭袋来完成。但是我想获得一个streamplot

# Field domain in polar coordinates:
R2, THETA2 = np.mgrid[1:3:10j, 0:2*np.pi:10j]

# Field components:
E_y = np.cos(THETA2)*np.sin(THETA2)*(2+3/R2**3)
E_z = (1+2/R2**3)*np.cos(THETA2)**2 + (1+1/R2**3)*np.sin(THETA2)**2

# Get domain in cartesian coordinates:
Z2 = R2*np.cos(THETA2)
Y2 = R2*np.sin(THETA2)

# Plot:
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.quiver(Z2, Y2, E_y, E_z)
ax2.streamplot(Z2, Y2, E_y, E_z)

我收到错误:ValueError: The rows of 'x' must be equal。我不知道自己做错了什么。

1 个答案:

答案 0 :(得分:1)

streamplot期望输入为笛卡尔坐标中的常规网格。您已在极坐标中创建了一个网格。要解决此问题,您可以使用笛卡尔坐标初始化网格,然后使用这些极坐标转换为极坐标,计算E_yE_z,然后绘制它。

# Grid in cartesian coordinates
xrange = np.linspace(-10, 10);
yrange = np.linspace(-10, 10);

xx,yy = np.meshgrid(xrange, yrange)

# Convert each point in grid to polar coordinates
R2 = np.sqrt(xx**2 + yy **2)
THETA2 = np.arctan2(xx, yy)

# Compute direction vectors
E_y = np.cos(THETA2)*np.sin(THETA2)*(2+3/R2**3)
E_z = (1+2/R2**3)*np.cos(THETA2)**2 + (1+1/R2**3)*np.sin(THETA2)**2

# Plot:
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.quiver(xx, yy, E_y, E_z)
ax2.streamplot(xx, yy, E_y, E_z)

enter image description here