python - python:将结构化但分散的数据转换为网格数据而不进行插值 - 快速
问题描述
我有一个数据数据框,其中包含 xy 和 z 坐标以及各种标量值。
数据实际上来自结构化网格,因为所有坐标都定义了具有恒定 x 和 y 间距(不一定相等)的矩形网格单元。目标是使用 tricontourf 绘制等高线图的数据(这没问题),然后使用 matplotlib 流线图函数添加流线。
唯一的问题是流图需要np.meshgrid
坐标、u 和 v 的格式网格。
所以我需要一种有效的方法将分散的数据转换为网格格式,而不使用scipy.interpolate.griddata
.
目前我有两个关于如何做到这一点的想法(一个正在使用griddata ...)
假设我的数据框如下所示:
print(thresh)
x y z f u v w r
616968 522625.0 5043701.5 3.75 0.000000 0.000000 0.000000 0.000000 0.000000
616969 522629.0 5043701.5 3.75 0.000000 0.000000 0.000000 0.000000 0.000000
616970 522633.0 5043701.5 3.75 0.000000 0.000000 0.000000 0.000000 0.000000
616971 522637.0 5043701.5 3.75 0.000000 0.000000 0.000000 0.000000 0.000000
616972 522641.0 5043701.5 3.75 0.000000 0.000000 0.000000 0.000000 0.000000
请注意,这是z 方向的切片,即所有z 坐标都相同。
1)
xmin = thresh['x'].min()
xmax = thresh['x'].max()
xstep = np.diff(thresh['x'])[0]
ymin = thresh['y'].min()
ymax = thresh['y'].max()
ystep = np.diff(thresh['y'])[0]
mg = np.meshgrid(np.arange(xmin, xmax+xstep, xstep), np.arange(ymin, ymax+ystep, ystep))
u = griddata(thresh[['x', 'y']].values, thresh['u'].values, (mg[0], mg[1]), method='linear')
v = griddata(thresh[['x', 'y']].values, thresh['v'].values, (mg[0], mg[1]), method='linear')
mg = np.meshgrid(np.unique(thresh['x']), np.unique(thresh['y']))
u = np.zeros(mg[0].shape)
v = np.zeros(mg[0].shape)
for row in range(u.shape[0]):
for col in range(u.shape[1]):
idx = np.where((thresh['x']==mg[0][row,col]) & (thresh['y']==mg[1][row,col]))[0].item()
u[row,col] = thresh.iloc[idx]['u']
v[row,col] = thresh.iloc[idx]['v']
从这里我现在可以根据需要进行绘图:
#note I have cut out some pointless code here
cut = (slice['r'] <= 0.003).values #data to NOT plot in contour
triang = Triangulation(thresh['x'], thresh['y'])
mask1 = np.all(np.where(cut[triang.triangles], True, False), axis=1)
triang.set_mask(mask1)
cnt = ax.tricontourf(triang, thresh['r'])
ax.streamplot(mg[0], mg[1], u, v)
任何关于如何使网格化步骤更有效的想法将不胜感激,或者如果有更简单的方法来实现我的最终目标
解决方案
推荐阅读
- sharepoint - SharePoint 文档库搜索框不工作 (InPlaceSearch)
- postgresql - Postgres:插入规则“返回列表的条目太少”
- javascript - Nodemailer:密码字符串包含“#”时出现连接超时错误
- grafana - Grafana 变量和 Prometheus 查询
- blockchain - Parity-ethereum 和 --pruning 如何快速处理古老的快照?
- javascript - 从 JSON 数据中获取特定值
- database - nodejs修改mongodb中的字符串
- node.js - 如何使用 arrayFilters 更新数组中的元素
- php - PHP - 在构建 [Spatie\Cors\Cors] 时不可实例化
- ios - PageviewController 问题中的水平滚动