15

I have Matplotlib version 1.5.1 and I am facing an interesting problem. I want to add an arrow to my plot, which would have heads on each end and specified colour and width. However, studying the Matplotlib documentation on this matter, I realized I probably can't have both. I can either have an arrow facing both ends, but this arrow will have default colour and linewidth - this is the option if I include arrowstyle in my arrowprops, or I can omit arrowstyle and set the colour and width in the arrow properties, but then I only have the default arrow. Is there a way to get both?

I have this code:

plt.annotate('', xy=(p[0][0]-p[0][2], 0), xycoords='data', xytext=(p[0][0], 0), textcoords='data', arrowprops=dict(arrowstyle: '<|-|>',color='k',lw=2.5))

which results in SyntaxError: invalid syntax.

(Note: p is simply a list of lists from which I get my x and y values, I am plotting in a loop)

durbachit
  • 4,626
  • 10
  • 36
  • 49

1 Answers1

28

You should be able to use arrowprops to set the color, width and other properties.

import matplotlib.pyplot as plt

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

ax.annotate('test', xy=(0.9, 0.9),
             xycoords='data',
             xytext=(0, 0),
             textcoords='data',
             arrowprops=dict(arrowstyle= '<|-|>',
                             color='blue',
                             lw=3.5,
                             ls='--')
           )

ax.set_xlim(-0.1,1)
ax.set_ylim(-0.1,1)
plt.show()

Gives this figure:enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Robbie
  • 4,672
  • 1
  • 19
  • 24
  • which version of matplotlib are you using? As I say, if I have `arrowstyle` in my `arrowprops`, then any `color`, `lw` and other `line2D` properties result in `invalid syntax` – durbachit Jun 12 '17 at 01:34
  • 1
    I'm using version 2.0.0. Note that there is a syntax error in the code you posted - you have "arrowstyle:..." when you should have "arrowstyle=..." – Robbie Jun 12 '17 at 01:35
  • The problem was carried from my previous version of the code, which had `arrowprops={'arrowstyle':'<|-|>'}` - so it turns out it was merely an error of a different syntax of a dictionary. – durbachit Jun 12 '17 at 01:50