today
today

Reputation: 33420

How to get arrow directions (i.e. U and V) in a matplotlib.pyplot.quiver plot?

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).

Upvotes: 1

Views: 745

Answers (1)

JohanC
JohanC

Reputation: 80339

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)

Upvotes: 1

Related Questions