Skip to content Skip to sidebar Skip to footer

How To Use Streamplot Function When 1d Data Of X-coordinate, Y-coordinate, X-velocity And Y-velocity Are Available?

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 a

Solution 1:

From the documentation:

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

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

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)):

enter image description here

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.

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:

enter image description here

Post a Comment for "How To Use Streamplot Function When 1d Data Of X-coordinate, Y-coordinate, X-velocity And Y-velocity Are Available?"