Skip to content Skip to sidebar Skip to footer

Matplotlib Quiver Key Label Getting Cut

When copying the example in the docs the label associated with the quiver key is not showing up. Any thoughts? Edit: Thanks all for your responses. @X helped me realize the differe

Solution 1:

My guess is that somehow your backend is changing the figure size on you and that moves the quiver key. Why do you want it in figure co-ordinates? Suggest you position it relative to the axes:

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, ax1 = plt.subplots()
ax1.set_title('Arrows scale with plot width, not view')
Q = ax1.quiver(X, Y, U, V, units='width')
qk = ax1.quiverkey(Q, 1.0, 1.02, 2, r'$2 \frac{m}{s}$', labelpos='E',
                   coordinates='axes')
plt.show()

Solution 2:

There is a difference between using a coordinate reference as an axis and a figure. The background color of the figure is pink and the background color of the axis is yellow.

qk = ax1.quiverkey(Q, 0.85, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E', coordinates='figure')

enter image description here

qk = ax1.quiverkey(Q, 0.85, 0.9, 2, r'$2 \frac{m}{s}$', labelpos='E', coordinates='axes')

enter image description here

Solution 3:

When in doubt, tight_layout! (it's a bit of a quick/easy fix, vs. fiddling forever with various margin attributes)

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, ax1 = plt.subplots()
ax1.set_title('Arrows scale with plot width, not view')
Q = ax1.quiver(X, Y, U, V, units='width')
qk = ax1.quiverkey(Q, 0.92, .95, 2, r'$2 \frac{m}{s}$', labelpos='E',
                   coordinates='figure')
plt.tight_layout()
plt.show()

enter image description here

Solution 4:

I believe you need to modify the coordinates of the key manually for different figsize. For the default (6,4):

qk = ax1.quiverkey(Q, 0.93, 0.93, 2, r'$2 \frac{m}{s}$', labelpos='E',
                   coordinates='figure')
plt.tight_layout()

gives:

enter image description here

Post a Comment for "Matplotlib Quiver Key Label Getting Cut"