1

I was wondering if there is any way to get the arrow directions (i.e. U and V) using the Axes object after a matplotlib.pyplot.quiver is plotted. For example:

fig, ax = plt.subplots()
plt.quiver(X=..., Y=..., U=..., V=...)
ax.collections[0].something_that_returns_UV?

I know that ax.collections[0].get_offsets() would return the X and Y values, but I am interested in the U and V values instead.

Use case: automated testing of plots (example).

today
  • 32,602
  • 8
  • 95
  • 115

1 Answers1

1

The collection created by ax.quiverkey is of type matplotlib.quiver.Quiver. You can obtain X, Y, U and V directly as ax.collections[0].X etc.

import matplotlib.pyplot as plt
import numpy as np

X, Y = np.meshgrid(np.arange(0, 2 * np.pi, .2), np.arange(0, 2 * np.pi, .2))
U = np.cos(X)
V = np.sin(Y)
fig1, ax = plt.subplots()
Q = ax.quiver(X, Y, U, V)

print('X:', ax.collections[0].X)
print('Y:', ax.collections[0].Y)
print('U:', ax.collections[0].U)
print('V:', ax.collections[0].V)
JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Ah, how silly of me, as I searched a lot and didn't find it! I thought a function like `get_offsets` should handle that. Thanks a lot! – today Aug 20 '20 at 06:34