当x坐标,y坐标,x速度和y速度的一维数据可用时,如何使用流图功能? [英] How to use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available?

查看:93
本文介绍了当x坐标,y坐标,x速度和y速度的一维数据可用时,如何使用流图功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前,有人在SO中提出过类似的问题,但我不明白那里提供的答案.因此,我再次提出了这个问题.我是这个论坛的新手.因此,如果我违反任何规则,我深表歉意.

A similar type of question has been asked in SO before, but I couldn't understand the answers provided there. Hence I am reposting the question again.I am new to this forum. So I apologize if I am violating any rules.

我从CFD仿真中获得了文本格式的原始数据.我想根据这些数据绘制流函数.关于streamplot函数的matplotlib文档提到:" u,v:二维数组x和y-速度.行数应与y的长度匹配,而列数应与x的长度匹配." 文本文件中的示例数据

I have obtained raw data in text format from my CFD simulation. I want to plot the stream function from this data. The matplotlib documentation for streamplot function mentions that "u, v : 2d arrays x and y-velocities. Number of rows should match length of y, and the number of columns should match x". This is the sample data from the text file

x            y         x-velocity   y-velocity
1.48E+01    0.00E+00    0.00E+00    2.36E-01
1.49E+01    0.00E+00    0.00E+00    2.36E-01
1.51E+01    0.00E+00    0.00E+00    2.36E-01
1.52E+01    0.00E+00    0.00E+00    2.36E-01
1.54E+01    0.00E+00    0.00E+00    2.36E-01
1.55E+01    0.00E+00    0.00E+00    2.36E-01
1.57E+01    0.00E+00    0.00E+00    2.36E-01
1.46E+01    1.52E-01    0.00E+00    0.00E+00
1.48E+01    1.52E-01    1.13E-04    2.36E-01
1.49E+01    1.52E-01    9.01E-05    2.36E-01
1.51E+01    1.52E-01    3.81E-05    2.36E-01
1.52E+01    1.52E-01    1.85E-13    2.36E-01
1.54E+01    1.52E-01    3.81E-05    2.36E-01
1.55E+01    1.52E-01    9.01E-05    2.36E-01
1.57E+01    1.52E-01    1.13E-04    2.36E-01
1.58E+01    1.52E-01    0.00E+00    0.00E+00
1.46E+01    3.05E-01    0.00E+00    0.00E+00
1.48E+01    3.05E-01    1.54E-04    2.36E-01
1.49E+01    3.05E-01    1.18E-04    2.36E-01
1.51E+01    3.05E-01    5.21E-05    2.36E-01
1.52E+01    3.05E-01    3.92E-12    2.36E-01
1.54E+01    3.05E-01    5.21E-05    2.36E-01
1.55E+01    3.05E-01    1.18E-04    2.36E-01
1.57E+01    3.05E-01    1.54E-04    2.36E-01
1.58E+01    3.05E-01    0.00E+00    0.00E+00
1.46E+01    4.57E-01    0.00E+00    0.00E+00
1.48E+01    4.57E-01    1.85E-04    2.36E-01
1.49E+01    4.57E-01    1.37E-04    2.36E-01
1.51E+01    4.57E-01    6.28E-05    2.36E-01

这是整个文本文件可用的链接.该数据是从等距网格中获得的. 文本文件. 如果仅对当前数据使用streamplot函数,它将显示一个异常' ValueError:'u'和'v'的形状必须为'Grid(x,y).我不确定如何进一步进行.我是否必须使用scipy中可用的一种插值方法来获得U和V速度的二维数组.

This is the link where the entire text file is available. This data was obtained from an equally spaced grid. Text File. If I just use the streamplot function with my current data, it shows up an exception'ValueError: 'u' and 'v' must be of shape 'Grid(x,y). I am not sure how to proceed further. Do I have to use one of the interpolation methods available in scipy to get a 2D array of U and V velocities.

我真的很困.有人可以帮我吗?

I am really stuck. Could anyone please help me on this?

谢谢你, 普拉莫特

推荐答案

从文档:

x,y :一维数组
均匀分布的网格.

x, y : 1d arrays
an evenly spaced grid.

u,v :二维数组
x和y速度.行数应与y的长度匹配,而列数应与x的长度匹配.

u, v : 2d arrays
x and y-velocities. Number of rows should match length of y, and the number of columns should match x.

查看您的数据,我可以立即看到您的xy值不是在等距网格上采样的,因为行数是奇数.这就是 quiver 图(即plt.quiver(x, y, u, v) ):

Looking at your data I can see straight away that your x and y values aren't sampled on an evenly spaced grid, since there is an odd number of rows. Here's what your data actually look like as a quiver plot (i.e. plt.quiver(x, y, u, v)):

所有向量都或多或少指向上,因为所有u值均比其相应的v值小至少三个数量级.

All the vectors point more or less straight upwards because all of your u values are at least 3 orders of magnitude smaller than their corresponding v values.

也许您发布的值只是整个数据集的一小部分,实际上可能是在常规网格上采样的.在这种情况下,您需要为网格中的每个列/行使xy唯一的x,y坐标,然后重塑uv的形状,以使它们各自的尺寸为(ny, nx).就目前而言,为了将这些数据绘制为流图,您需要在xy坐标的常规2D网格上对uv进行重新采样.

Perhaps the values you posted are only a small fraction of the whole dataset, which might in fact be sampled on a regular grid. In that case you would need to make x and y the unique x,y coordinates for each column/row in the grid, then reshape u and v so that they each have dimensions (ny, nx). As it stands, in order to plot these data as a stream plot you would need to resample u and v on a regular 2D grid of x and y coordinates.

一种选择是尝试在一组新的网格位置上进行插值,例如使用

One option would be to try interpolating them at a new set of grid locations, for example using scipy.interpolate.griddata:

import numpy as np
from scipy.interpolate  import griddata

# resample onto a 50x50 grid
nx, ny = 50, 50

# (N, 2) arrays of input x,y coords and u,v values
pts = np.vstack((x, y)).T
vals = np.vstack((u, v)).T

# the new x and y coordinates for the grid, which will correspond to the
# columns and rows of u and v respectively
xi = np.linspace(x.min(), x.max(), nx)
yi = np.linspace(y.min(), y.max(), ny)

# an (nx * ny, 2) array of x,y coordinates to interpolate at
ipts = np.vstack(a.ravel() for a in np.meshgrid(yi, xi)[::-1]).T

# an (nx * ny, 2) array of interpolated u, v values
ivals = griddata(pts, vals, ipts, method='cubic')

# reshape interpolated u,v values into (ny, nx) arrays
ui, vi = ivals.T
ui.shape = vi.shape = (ny, nx)

# plot
fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.streamplot(xi, yi, ui, vi)
ax.quiver(x, y, u, v)

结果不是很好,但是与颤动图显示的一致:

The result isn't pretty, but it's consistent with what the quiver plot shows:

这篇关于当x坐标,y坐标,x速度和y速度的一维数据可用时,如何使用流图功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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