Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal-sized arrowheads in matplotlib

I want to plot arrows with matplotlib so that every arrowhead is of equal size, regardless of of the actual arrow's size.

Examples:

  • current arrowheads:
    1
  • desired arrowheads:
    2

The arrows are drawn using matplotlib.pyplot.Arrow():

# p is the point of origin, pdiff the direction        
arr = plt.Arrow(p[0], p[1], pdiff[0], pdiff[1], fc=color, width=0.4)
plt.gca().add_patch(arr)
like image 274
dassmann Avatar asked Dec 18 '12 20:12

dassmann


1 Answers

You are after pylab.arrow (or FancyArrow), then you can specify head_width and head_length so they are not relative to the size of the arrow. Here is an example:

import math
import pylab

pylab.plot(range(11), range(11))

opt = {'head_width': 0.4, 'head_length': 0.4, 'width': 0.2,
        'length_includes_head': True}
for i in xrange(1, 360, 20):
    x = math.radians(i)*math.cos(math.radians(i))
    y = math.radians(i)*math.sin(math.radians(i))

    # Here is your method.    
    arr = pylab.Arrow(4, 6, x, y, fc='r', alpha=0.3)
    pylab.gca().add_patch(arr)

    # Here is the proposed method.
    pylab.arrow(4, 6, x, y, alpha=0.8, **opt)

pylab.show()

Which produces:

enter image description here

like image 154
mmgp Avatar answered Oct 03 '22 00:10

mmgp