不均匀(曲线)网格的Matplotlib Streamplot

2024-09-28 21:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着在曲线网格上绘制流线,但我得到了错误,最后:

/usr/local/lib/python2.7/dist-packages/matplotlib/streamplot.pyc in streamplot(axes, x, y, u, v, density, linewidth, color, cmap, norm, arrowsize, arrowstyle, minlength, transform)
    100 
    101     ## Sanity checks.
--> 102     assert u.shape == grid.shape
    103     assert v.shape == grid.shape
    104 

AssertionError: 

我做了matplotlib示例,它运行得很好。如何将流线绘制到该网格? 密码在下面,你可以试试。这对我来说真的很重要。在

^{pr2}$

Result of the code


Tags: matplotliblibpackagesusrlocaldist错误绘制
1条回答
网友
1楼 · 发布于 2024-09-28 21:26:26

感谢Shahar Shani Kadmiel

此函数可解决以下问题:

from scipy.interpolate import griddata

def streams(ax,xx,yy,u,v,base_map=False):
 x = np.linspace(xx.min(), xx.max(), 50)
 y = np.linspace(yy.min(), yy.max(), 50)

 xi, yi = np.meshgrid(x,y)

 #then, interpolate your data onto this grid:

 px = xx.flatten()
 py = yy.flatten()
 pu = u.flatten()
 pv = v.flatten()
 pspeed = speed.flatten()

 gu = griddata(zip(px,py), pu, (xi,yi))
 gv = griddata(zip(px,py), pv, (xi,yi))
 gspeed = griddata(zip(px,py), pspeed, (xi,yi))

 lw = 6*gspeed/np.nanmax(gspeed)
 #now, you can use x, y, gu, gv and gspeed in streamplot:

 if base_map:
    xx,yy = ax(xx,yy)
    xi,yi = ax(xi,yi)

 ax.contour(xx,yy,speed, colors='k', alpha=0.4)
 ax.plot(xx,yy,'-k',alpha=0.3)
 ax.plot(xx.T,yy.T,'-k',alpha=0.3)
 ax.plot(xi,yi,'-b',alpha=0.1)
 ax.plot(xi.T,yi.T,'-b',alpha=0.1)
 c = ax.streamplot(x,y,gu,gv, density=2,
    linewidth=lw, color=gspeed, cmap=plt.cm.jet)

相关问题 更多 >