How to draw an arrow using matplotlib

Dear friends, I just start to use matplotlib, which looks

    > quite promising for me. I need to draw a couple of arrows
    > in my 2D plot. Is there a simple way to get it work?

    > Any suggustions are welcome.

I recommend creating an arrow class, derived from matplotlib.artist.Artist, that
contains a matplotlib.lines.Line2D for the arrow stem and a
matplotlib.patches.RegularPolygon with numVertices=3 for the arrow
head. You can control the rotation of the arrowhead with the
orientation argument.

Once you have this class so defined, you can add it instances of it to
the axes with ax.add_artist(arrow).

I'll be happy to help out with a prototype if you have trouble. Take
a look at matplotlib.table.Cell, which John Gill wrote to support
tables. You can use this as a simple model for how to write new
artists (things that draw into a figure) composed of other artists.

It would be nice to have a fancy arrow class, that supported text
labeling, at the base, along the stem and at the arrowhead. You could
also consider a more sophisticated polygon other than a triangle for
the arrowhead.

Finally, if you needed to draw *a lot of arrows*, order of a thousand
or more (eg for direction fields), a
matplotlib.collections.PolygonCollection would be the way to go for
efficiency.

JDH

Thanks a lot for the very very detail reply. I can not find the time to do it by myself at the moment. I will come back to this issue again after 2 two weeks.

Best regards,
Chris

John Hunter wrote:

···

"Chris" == Chris <Ch.Shen@...385...> writes:

    > Dear friends, I just start to use matplotlib, which looks
    > quite promising for me. I need to draw a couple of arrows
    > in my 2D plot. Is there a simple way to get it work?

    > Any suggustions are welcome.

I recommend creating an arrow class, derived from matplotlib.artist.Artist, that
contains a matplotlib.lines.Line2D for the arrow stem and a
matplotlib.patches.RegularPolygon with numVertices=3 for the arrow
head. You can control the rotation of the arrowhead with the
orientation argument.

Once you have this class so defined, you can add it instances of it to
the axes with ax.add_artist(arrow).

I'll be happy to help out with a prototype if you have trouble. Take
a look at matplotlib.table.Cell, which John Gill wrote to support
tables. You can use this as a simple model for how to write new
artists (things that draw into a figure) composed of other artists.

It would be nice to have a fancy arrow class, that supported text
labeling, at the base, along the stem and at the arrowhead. You could
also consider a more sophisticated polygon other than a triangle for
the arrowhead.

Finally, if you needed to draw *a lot of arrows*, order of a thousand
or more (eg for direction fields), a
matplotlib.collections.PolygonCollection would be the way to go for
efficiency.

JDH

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click

I tried with what you suggested to make an Arrow class. To begin with, I only put an Line2D instance. I searched for table stuff in axes.py, matlab.py to make the arrow importable from matplotlib.matlab. Then I also try a very very simple demo and I expect to see a simple line. However, I can not see it. Could someone tell me what is the problem. Here is my very very rudimental codes.

···

-----------------------------------------------------------------
arrow.py

from artist import Artist
from lines import Line2D
from patches import RegularPolygon

class Arrow(Artist):
  """
  An arrow with an triangular head .

  """
  def __init__(self, xdata=None, ydata=None, *args, **kwargs):
    
    Artist.__init__(self)
    print xdata, ydata
    self._stem = Line2D(xdata, ydata, *args, **kwargs)
    
  def draw(self, renderer):
    self._stem.draw(renderer)

------------------------------------------------------------
in file axes.py
   in function cla add:
         self.arrows =
---------------------------
     def add_arrow(self, a):
         'Add an arrow instance to the list of axes arrows'
         self._set_artist_props(a)
         self.arrows.append(a)

     def arrow(self, x, y, *args, **kwargs):
         a = Arrow(x,y, *args, **kwargs)
         self.add_arrow(a)
         return a
-------------------------------------------------------------
in file matlab.py
  add two tring for importing arrow
-------------------------------------------------------------
arrow_demo.py

from matplotlib.matlab import *

x = [0,1]
y = [0,1]
arrow(x,y, color='b',linestyle='-')
show()
-------------------------------------------------------------

Best regards,
Chris

John Hunter wrote:

I recommend creating an arrow class, derived from matplotlib.artist.Artist, that
contains a matplotlib.lines.Line2D for the arrow stem and a
matplotlib.patches.RegularPolygon with numVertices=3 for the arrow
head. You can control the rotation of the arrowhead with the
orientation argument.

Once you have this class so defined, you can add it instances of it to
the axes with ax.add_artist(arrow).

I'll be happy to help out with a prototype if you have trouble. Take
a look at matplotlib.table.Cell, which John Gill wrote to support
tables. You can use this as a simple model for how to write new
artists (things that draw into a figure) composed of other artists.

It would be nice to have a fancy arrow class, that supported text
labeling, at the base, along the stem and at the arrowhead. You could
also consider a more sophisticated polygon other than a triangle for
the arrowhead.

Finally, if you needed to draw *a lot of arrows*, order of a thousand
or more (eg for direction fields), a
matplotlib.collections.PolygonCollection would be the way to go for
efficiency.

JDH

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE
LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click