## matplotlib-users

 [Matplotlib-users] How to draw an arrow using matplotlib From: Chris - 2004-11-04 11:10:08 ```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. Best regards, Shen ```
 Re: [Matplotlib-users] How to draw an arrow using matplotlib From: John Hunter - 2004-11-06 22:40:43 ```>>>>> "Chris" == Chris writes: Chris> Dear friends, I just start to use matplotlib, which looks Chris> quite promising for me. I need to draw a couple of arrows Chris> in my 2D plot. Is there a simple way to get it work? Chris> 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 ```
 [Matplotlib-users] Re: How to draw an arrow using matplotlib From: Chris - 2004-11-08 08:20:27 ```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 writes: > > > Chris> Dear friends, I just start to use matplotlib, which looks > Chris> quite promising for me. I need to draw a couple of arrows > Chris> in my 2D plot. Is there a simple way to get it work? > > Chris> 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 ```
 [Matplotlib-users] Re: How to draw an arrow using matplotlib From: Chris - 2004-11-16 16:08:20 ```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 ```
 Re: [Matplotlib-users] Re: How to draw an arrow using matplotlib From: John Hunter - 2004-11-16 16:14:31 ```>>>>> "Chris" == Chris writes: Chris> I tried with what you suggested to make an Arrow class. To Chris> begin with, I only put an Line2D instance. I searched for Chris> table stuff in axes.py, matlab.py to make the arrow Chris> importable from matplotlib.matlab. Then I also try a very Chris> very simple demo and I expect to see a simple Chris> line. However, I can not see it. Could someone tell me what Chris> is the problem. Here is my very very rudimental codes. Nowhere in your code do you actually draw the arrows in self.arrows. In Axes.draw you would need to do for arrow in self.arrows: arrow.draw(renderer) And this should work. However, you can avoid all the overhead of add_arrow, and keeping a list of arrows in self.arrows simply by doing def arrow(self, x, y, *args, **kwargs): a = Arrow(x,y, *args, **kwargs) self.add_artist(a) ^^^ return a Ie, call add_artist which is a generic method for adding artists to the axes. Hope this helps, JDH ```
 [Matplotlib-users] Re: How to draw an arrow using matplotlib From: Chris - 2004-11-16 16:47:37 ```Dear John, I see your points. However, after I changed to using add_artist. This is still nothing on the canvas. I think that there is some problem with the draw function of my arrow class. I have no idea what else should be there. At the moment, only one call in my case as below. def draw(self, renderer): self._stem.draw(renderer) Could u tell what else should be called? Best regards, Chris John Hunter wrote: >>>>>>"Chris" == Chris writes: > > > Chris> I tried with what you suggested to make an Arrow class. To > Chris> begin with, I only put an Line2D instance. I searched for > Chris> table stuff in axes.py, matlab.py to make the arrow > Chris> importable from matplotlib.matlab. Then I also try a very > Chris> very simple demo and I expect to see a simple > Chris> line. However, I can not see it. Could someone tell me what > Chris> is the problem. Here is my very very rudimental codes. > > Nowhere in your code do you actually draw the arrows in self.arrows. > In Axes.draw you would need to do > > for arrow in self.arrows: > arrow.draw(renderer) > > And this should work. > > However, you can avoid all the overhead of add_arrow, and keeping a > list of arrows in self.arrows simply by doing > > > def arrow(self, x, y, *args, **kwargs): > a = Arrow(x,y, *args, **kwargs) > self.add_artist(a) > ^^^ > return a > > Ie, call add_artist which is a generic method for adding artists to > the axes. > > Hope this helps, > JDH > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: InterSystems CACHE > FREE OODBMS DOWNLOAD - A multidimensional database that combines > robust object and relational technologies, making it a perfect match > for Java, C++,COM, XML, ODBC and JDBC. http://www.intersystems.com/match8 ```
 Re: [Matplotlib-users] Re: How to draw an arrow using matplotlib From: John Hunter - 2004-11-16 16:58:19 ```>>>>> "Chris" == Chris writes: Chris> Dear John, I see your points. However, after I changed to Chris> using add_artist. This is still nothing on the canvas. I Chris> think that there is some problem with the draw function of Chris> my arrow class. I have no idea what else should be Chris> there. At the moment, only one call in my case as below. Chris> def draw(self, renderer): self._stem.draw(renderer) Chris> Could u tell what else should be called? Your drawing function looks fine. Could this be a problem with axes limits. Make sure the x and y limits are set properly with the xlim, ylim or axis commands. The limits are normally autoscaled by calling plot, but you are not using it so you have to take care that the limits are set. After you get a working prototype, I can help you with the autoscaling part. If this doesn't fix your problem, email me your matlab.py, axes.py and test code as attachments off-list and I'll take a look. JDH ```