Jan Klaas
Jan Klaas

Reputation: 371

How does matplotlibs quiver work?

I'm trying to understand Quiver by trial and error but my math is not so on point. I was wondering if anyone could shed some light on this.

I have a histogram as a matrix like this:

magnitude, angle = [[3,2,4,3,9], [x,x,x,x,x]]

Angle is not yet determined but on the histogram the Angle are the bins and the magnitude is the Y. The first bin would represent the first 1/5 of the circle and thus the Angle there.

The problem however why i've not yet filled the angle is because I want to draw 5 lines/vectors from one point with plt.quiver.

plt.quiver(x,y,u,v) 

X and Y speaks for itself but I cannot figure out where u and v stands for. The documentation doesn't say much either. I need to draw 5 lines from the same point with a certain angle and magnitude which is given in the histogram but I cannot get it to work. Am i even using the function the right way? Thanks !!

btw I'm making a hog(histogram of gradients) and the result should look like this:

enter image description here

Upvotes: 1

Views: 3790

Answers (2)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339120

The call signature of quiver is quiver(X, Y, U, V).
It often helps to reduce the problem to a minimal example. Consider the following code:

import matplotlib.pyplot as plt

fig, ax=plt.subplots()

ax.quiver([0,1.5],[.5,1.5],[1,-0.5],[1,-1],
          angles='xy', scale_units='xy', scale=1.)

ax.set_xlim([-1,2])
ax.set_ylim([0,2])
plt.show()

enter image description here

This code adds two arrows. The first starts at point (x=0, y=0.5), the second starts at (1.5, 1.5). The first arraw goes in direction (u=1,v=1), the second in direction (-0.5,-1).

Now in this case we set scale_units='xy' and scale=1 such that u and v are in data coordinates and the arrow's endpoint is (x0+u0, y0+v0). In case we chose the scale units differently, the length of the arrow would be different, but the direction would be the same.

So if you call quiver(X, Y, U, V), you plot len(U) different arrows; the ith arrow starting at X[i], Y[i], and pointing in direction U[i],Y[i].

This means that in order to plot 5 arrows per position, you might call quiver five times, with the same grid arguments X and Y but with differing U and V.

Upvotes: 5

Cory Kramer
Cory Kramer

Reputation: 117856

From the matplotlib.axes.Axes.quiver documentation

Call signatures

quiver(U, V, **kw)
quiver(U, V, C, **kw)
quiver(X, Y, U, V, **kw)
quiver(X, Y, U, V, C, **kw)

Parameters
X : The x coordinates of the arrow locations
Y : The y coordinates of the arrow locations
U : The x components of the arrow vectors
V : The y components of the arrow vectors

So essentially (x, y) denotes the location of each quiver, and (u, v) denote the orientation of the vector.

Upvotes: 1

Related Questions