Reputation: 23
This is the plot of my vector field:
I want the vectors of a velocity field to be evenly spaced throughout this figure, instead of being squished together in the x direction, and stretched out in the y direction.
I think I know why it goes wrong, but how do I make it go right? Here is the relevant part of the code.
slice_interval = 10
skip = slice(None, None, slice_interval)
plt.quiver(x[skip], y[skip], u[skip], v[skip])
Update
Based on the suggested solution, doing x[skip][skip]
, etc..., did not solve the issue. If anything it actually made it worse. But it may provide some possibilities to tinker around with, maybe different skips for each of the axes? I'll try to tinker around with it some on my own.
New graph after trying suggested solution:
New Update
I did not implement the solution correctly the first time, after a lot of fiddling I now get a beautiful velocity field plot, thanks a lot for the help and encouragement, in particular xg.plt.py for the solution.
Corrected plot:
Upvotes: 2
Views: 678
Reputation: 8783
If you want them to be evenly spaced both in x and y axis, you have to slice the input arrays in both of their dimensions. Otherwise, you are slicing per rows.
The only line that needs to be changed is:
plt.quiver(x[skip,skip], y[skip,skip], u[skip,skip], v[skip,skip])
As pointed out in the comments, another option is to define skip2 = (slice(None, None, slice_interval),) * 2
and slicing the arrays directly with array[skip2]
gives exactly the same result.
A different case is doing array[skip][skip]
. In this case, the slicing is applied and then the result of the first slice is sliced again, the second [skip]
is not applied to the second axis of the array but instead _at the first axis of array[skip]
. Step by step it is doing:
# we start from array.shape = (110, 100)
a2 = array[skip]
# Intermediate step: a2.shape = (11, 100) # containing rows 0, 10, 20...
a3 = a2[skip]
# Final output: a3.shape = (2,100) # now we have sliced every 10 rows of a2
# which translated to array means slicing every 100 rows and still keeping all the columns.
Below there is a plot showing each of the interesting cases (both u and v are set to 1 so that all vectors are equal, and the meshgrid is created with a 100 dots linspace between 0 and 100 in x and 110 dots between -50 and 50 in y)
Upvotes: 1