kungphil
kungphil

Reputation: 1839

Alter the width of an annotate arrow in matplotlib

I am plotting an arrow in matplotlib using annotate. I would like to make the arrow fatter. The effect I am after is a two-headed arrow with a thin edge line where I can control the arrow width i.e. not changing linewidth. I have tried kwargs such as width after this answer but this caused an error, I have also tried different variations of arrowstyle and connectorstyle again without luck. I'm sure it's a simple one!

My code so far is:

import matplotlib.pyplot as plt

plt.figure(figsize=(5, 5))
plt.annotate('', xy=(.2, .2),  xycoords='data',
            xytext=(.8, .8), textcoords='data',
            arrowprops=dict(arrowstyle='<|-|>',
                            facecolor='w',
                            edgecolor='k', lw=1))
plt.show()

I am using Python 2.7 and Matplotlib 1.5.1

Upvotes: 3

Views: 5120

Answers (1)

Ianhi
Ianhi

Reputation: 3152

The easiest way to do this will be by using FancyBboxPatch with the darrow (double arrow) option. The one tricky part of this method is that the arrow will not rotate around its tip but rather the edge of the rectangle defining the body of the arrow. I demonstrate that with a red dot placed at the rotation location.

import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib as mpl

fig = plt.figure()
ax = fig.add_subplot(111)

#Variables of the arrow
x0 = 20
y0 = 20
width = 20
height = 2
rotation = 45
facecol = 'cyan'
edgecol = 'black'
linewidth=5

# create arrow
arr = patches.FancyBboxPatch((x0,y0),width,height,boxstyle='darrow',
                             lw=linewidth,ec=edgecol,fc=facecol)

#Rotate the arrow. Note that it does not rotate about the tip
t2 = mpl.transforms.Affine2D().rotate_deg_around(x0,y0,rotation) + ax.transData


plt.plot(x0,y0,'ro') # We rotate around this point
arr.set_transform(t2) # Rotate the arrow


ax.add_patch(arr)

plt.xlim(10, 60)
plt.ylim(10, 60)

plt.grid(True)

plt.show()

Giving:

enter image description here

Upvotes: 1

Related Questions