Inserting arrows in matplotlib

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    Inserting arrows in matplotlib

    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
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    According to matplotlib docs, arrow usage is:
    Code:
    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.

    Comment

    • Glenton
      Recognized Expert Contributor
      • Nov 2008
      • 391

      #3
      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.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        I just found a link about FancyArrowPatch here:


        which might be helpful.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

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

          BV

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

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

            Code:
            from matplotlib.patches import FancyArrowPatch
            ax=gca()
            ax.add_patch(FancyArrowPatch((9,0),(9,0.5),arrowstyle='->',mutation_scale=30))
            And it worked brilliantly!

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Glenton,

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

              BV

              Comment

              • Glenton
                Recognized Expert Contributor
                • Nov 2008
                • 391

                #8
                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...)

                Comment

                • Anon Coward

                  #9
                  It seems that this still doesn't work. A simple example:

                  arrow(0.2,0.2,0 .6,0.6)

                  This should produce a diagonal line with an arrow-head on the end. It produces a diagonal line with no arrow-head.

                  The example code which explicitly imports FancyArrowPatch didn't work for me, either - it produced absolutely nothing.

                  Comment

                  • Glenton
                    Recognized Expert Contributor
                    • Nov 2008
                    • 391

                    #10
                    Hi. Please post your code, and exactly what happened. Eg when you say nothing, does it mean no graph or no arrow?

                    Comment

                    Working...