Reputation: 9844
I'm plotting a vector field with the quiver method of Matplotlib.
My array to store this vector has a dimension x * y but I'm working with a space that varies from -2 to 2.
So far, to plot the vector field I have this method:
import matplotlib.pyplot as plt
def plot_quiver(vector_field_x, vector_field_y, file_path):
plt.figure()
plt.subplots()
plt.quiver(vector_field_x, vector_field_y)
plt.savefig(file_path + '.png')
plt.close()
Which gives me this output, as an example, for a 10 x 10 array:
But to generate this vector field I centered my data in the x = 0, y = 0, x and y ranging from -2 to 2. Then, I would like to plot the axis of the image following this pattern.
As an standard approach, I tried to do the following:
def plot_quiver(vector_field_x, vector_field_y, file_path):
plt.figure()
fig, ax = plt.subplots()
ax.quiver(vector_field_x, vector_field_y)
ax.set_xticks([-2, 0, 2])
ax.set_yticks([-2, 0, 2])
plt.savefig(file_path + '.png')
plt.close()
Which usually works with Matplotlib methods, as imshow and streamplot, for example.
But this what I've got with this code:
Which is not what I want.
So, I'm wondering how can I perform what I explained here to change the axes ticks.
Thank you in advance.
Upvotes: 1
Views: 1455
Reputation: 3964
Funny thing, I just learnt about quiver
yesterday... :)
According to the quiver documentation, the function can accept from 2 to 5 arguments...
The simplest way to use the function is to pass it two arrays with equal number of elements U
and V
. Then, matplotlib will plot an arrow for each element in the arrays. Specifically, for each element i,j
you will get an arrow placed at i,j
and with components defined by U[i,j]
and V[i,j]
. This is what is happening to you
A more complete syntax is to pass our arrays with equal number of elements X
, Y
, U
and V
. Again, you will get an arrow for each i,j
element with components defined by U[i,j]
and V[i,j]
, but this time they will be placed at coordinates X[i,j]
, Y[i,j]
.
you need to call quiver
like
quiver(values_x, values_y, vector_field_x, vector_field_y)
Probably you already did it, but you can get values_x
and values_y
using the numpy.meshgrid
function.
The matplotlib example for the quiver
function might be useful, also.
I hope it helps!
Upvotes: 1