ztl
ztl

Reputation: 2592

Background color of quiverkey in matplotlib

How can I define the background color of a quiverkey added to a quiver plot in matplotlib? In the following example, how can I make it white in order to have it more readable (even if it overrides part of the figure)?

import matplotlib.pyplot as plt
import numpy.random

numpy.random.seed(2)
x,y = numpy.meshgrid(range(11), range(11))
u,v = numpy.random.randn(101), numpy.random.randn(101)

qv = plt.quiver(x,y,u,v)
plt.quiverkey(qv, .5, 0.8, 5, 'Where is my background?', coordinates='axes',
              fontproperties={'weight': 'bold'}, color='r', labelcolor='g')

enter image description here

Upvotes: 1

Views: 4610

Answers (1)

Ed Smith
Ed Smith

Reputation: 13206

You can change the background color in the text attribute of the quiverkey using the set_backgroundcolor function. A complete example,

import matplotlib.pyplot as plt
import numpy.random

numpy.random.seed(2)
x,y = numpy.meshgrid(range(11), range(11))
u,v = numpy.random.randn(101), numpy.random.randn(101)

qv = plt.quiver(x,y,u,v)
qk = plt.quiverkey(qv, .5, 0.8, 5, 'Here is my background', coordinates='axes',
              fontproperties={'weight': 'bold'}, color='r', labelcolor='g')
t = qk.text.set_backgroundcolor('w')
plt.show()

enter image description here

Upvotes: 3

Related Questions