0

So I have found two previous similar questions asked here:

How to use streamplot function when 1D data of x-coordinate, y-coordinate, x-velocity and y-velocity are available?

How to plot Streamlines with Matplotlib given 1-D arrays of X cords, Y cords, U components and V components

The first question deals with arrays of different sizes (which isn't my case, X, Y, U and V will always be of the same length in my example) while the second does provide some more headway becomes incomprehnsible later on in the question and doesn't provide a solution.

Moving onto my problem I have 4, 1-D arrays, the X coordinates and Y coordinates of where each vector is and then the respective U and V values for each vector. I am trying to visualise the vector field (which I can visualise correctly in .quiver) as a streamline visualization using streamplot but I encounter the problem of making U and V 2D. I don't fully understand what the second dimension needs to contain for U and V so any clarification (and code ideally would be great).

The only code I could provide is my implementation of the second link but that doesn't work for me so would be obselete.

1
  • "that doesn't work for me so would be obselete" Or maybe your implementation is wrong, or your data is not suitable, how could we tell? Commented Mar 8, 2016 at 21:01

1 Answer 1

3

Use griddata (see also scipy.interpolate.griddata) to interpolate 1D data to a 2D grid.

import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate

# lowercase variables are 1D arrays
x = np.linspace(0, 2 * np.pi, 10)
y = np.sin(x)
u = np.cos(x)
v = np.sin(x)

# capitalized variables are 2D arrays
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X, Y = np.meshgrid(xi, yi)
U = interpolate.griddata((x, y), u, (X, Y), method='cubic')
V = interpolate.griddata((x, y), v, (X, Y), method='cubic')

plt.figure()
plt.quiver(x, y, u, v, scale_units='xy', angles='xy', scale=1.5)
plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)
plt.show()

enter image description here


import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interpolate

# lowercase variables are 1D arrays
x = np.array([1,2,3,4,5])
y = np.array([3,1,5,1,3])
u = np.array([1,1,0,-1,-1])
v = np.array([-0.5,1,-1,1,-0.5])

# capitalized variables are 2D arrays
xi = np.linspace(x.min(), x.max(), 100)
yi = np.linspace(y.min(), y.max(), 100)
X, Y = np.meshgrid(xi, yi)
U = interpolate.griddata((x, y), u, (X, Y), method='nearest')
V = interpolate.griddata((x, y), v, (X, Y), method='nearest')

plt.figure()
plt.quiver(x, y, u, v, scale_units='xy', angles='xy', scale=1.5)
plt.streamplot(X, Y, U, V, color=U**2+V**2, linewidth=2, cmap=plt.cm.autumn)
plt.show()

yields

enter image description here

Sign up to request clarification or add additional context in comments.

6 Comments

Fantastic! I'm at least getting streamlines now although the result isn't what was expected. I know this is now outside the domain of the question originally asked but is there an obvious reason why without seeing the data? If not I will spend more time analysing the data to determine how it isn't working.
What is the expected result? You may also want to try using method='nearest' in the calls to interpolate.griddata. The result won't look as smooth, but you'll get stream lines throughout the gridded domain. To get cubic interpolation throughout the gridded domain, you need 1D data at the boundary of the gridded domain.
Expected would be a symmetric set of streamlines on the two sides of the graph. The boundary of the gridded domain is found on where the vectors are placed, the surrounding space until the image boundary is not included in the calculations and is only used to help visual debugging. I tried method='nearest' but received an error "only integer arrays with one element can be converted to an index". Despite a few problems still encountered you have been a tremendous help!
I've added a second example, with guesses for x, y, u, v to roughly simulate your data. If you get the same result as I do when you run the code above, then it may help to compare how your real data differs from the guesses I used.
I can reproduce the error message TypeError: only integer arrays with one element can be converted to an index if x, y, u and v are Python lists instead NumPy arrays. Be sure to convert them to NumPy arrays: x = np.array(x), etc.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.