Connecting Tech Pros Worldwide Help | Site Map

Inserting arrows in matplotlib

Member
 
Join Date: Nov 2008
Posts: 49
#1: Sep 24 '09
Hi All

I've been knocking my head against this, and wondered if anyone had any insights.
I've made a plot with the wonderful matplotlib. I've always found it to work brilliantly, and I've found that even without knowing much about it I can get it to do most of the things I've wanted it to do.

Today, I wanted to insert a few arrows onto my graphs to point out some features. They didn't need any text or other annotation. Just the simplest imaginable vertical arrows. Imagine my surprise, when this turned out to be impossible for me to do.

I tried various things with arrow and annotate. With arrow I managed to get a thick vertical line where I wanted the arrow. But I couldn't figure out how to make it look like an arrow. I realised that the arrows it was trying to draw were a lot fancier than I needed, but I tried various combinations and nothing. I feel really stupid at this point.

So let's make it more specific. A set of axes with x running from 200 to 300, and y running from 0.5 to 1.5. I want something that looks like a little arrow pointing down, so the tip is at (250, 1.3) and the tail is at (250,1.45) say.
Any ideas of how this could be done? It must be simple, surely!

The next level of complication (although just achieving the above would cause great joy), is that sometimes I'd like the arrow to go beyond the bounds of the axes. Say (260,1.45) to (260,1.6). Any ideas?

Lastly, can I just impose the arrows on the whole figure, rather than on a specific set of axes, so that I can easily have arrows the same size regardless of the number of subplots and what their scales are? Of course this is the least important, because it can be calculated...

Many thanks!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#2: Sep 24 '09

re: Inserting arrows in matplotlib


According to matplotlib docs, arrow usage is:
Expand|Select|Wrap|Line Numbers
  1. arrow(x, y, dx, dy, **kwargs)
which will draw an arrow on specified axis from (x, y) to (x + dx, y + dy). Optional kwargs control the arrow properties. Check out the example source code.
Member
 
Join Date: Nov 2008
Posts: 49
#3: Sep 24 '09

re: Inserting arrows in matplotlib


thanks, bvdet. That's what I've been looking at the whole day. So the basic usage, without any optional arguments, is exactly how I managed to get some lines on my graph. And if I made them more angled, I could just make out the arrow head. But unfortunately that's as far as I could take it. I tried fiddling with the key-word arguments (width, and head_width), but I just couldn't make it work. The example is pretty complicated, and unfortunately leaves me none the wiser. There's a much simpler example, which I thought I might have more luck with, which uses annotate. But again, it was unclear from the examples what certain parameters do, and I had no joy trying to fix it.

I guess I'll just keep looking. Thanks anyway.
Member
 
Join Date: Nov 2008
Posts: 49
#4: Sep 24 '09

re: Inserting arrows in matplotlib


I just found a link about FancyArrowPatch here:
http://tkjacobsen.wordpress.com/cate...on/matplotlib/

which might be helpful.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#5: Sep 24 '09

re: Inserting arrows in matplotlib


Thanks for the update Glenton. I have never used matplotlib, so I am learning about this with you!

BV
Member
 
Join Date: Nov 2008
Posts: 49
#6: Sep 28 '09

re: Inserting arrows in matplotlib


Okay, I've just tried the code in the post

Expand|Select|Wrap|Line Numbers
  1. from matplotlib.patches import FancyArrowPatch
  2. ax=gca()
  3. ax.add_patch(FancyArrowPatch((9,0),(9,0.5),arrowstyle='->',mutation_scale=30))
And it worked brilliantly!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,561
#7: Sep 28 '09

re: Inserting arrows in matplotlib


Glenton,

You have gotten me interested in matplotlib. I will try it out when I get a chance.

BV
Member
 
Join Date: Nov 2008
Posts: 49
#8: Sep 28 '09

re: Inserting arrows in matplotlib


it is rapidly becoming my favourite way to do graphs. I did an animation for the wikipedia article on the quantum hall effect the other day with it (I still need to fix the article when I get some time, and make the graph a bit smaller...)
Reply

Tags
annotate, arrow, matplotlib, patches