How to create some derived arrow classes with matplotlib and python
Here is an example of how to create some derived arrow classes with matplotlib and python. The arrow() function in matplotlib accepts origin and delta x and delta y inputs. I changed this to polor coordinates so Arrow2 accepts the x and y coordinates of the origin, the length, and the angle. Then I created 4 classes derived from Arrow2 called ArrowRight, ArrowLeft, ArrowUp, and ArrowDown. These just set the angle for you to 0, 180, 90, and 270 respectively. Notice too that the **kwargs can be passed down so you can still set all the other parameters.
""" arrow_ex2.py """
from pylab import *
def main():
figure()
axes()
Arrow2(.5,.5,.2,45)
ArrowRight(.5,.5,.2)
ArrowLeft(.5,.5,.2)
ArrowUp(.5,.5,.2)
ArrowDown(.5,.5,.2)
show()
class Arrow2:
def __init__(self, x0, y0, length, angle=0.0, color='k', width=0.01, **kwargs):
dx = length*cos(angle*pi/180)
dy = length*sin(angle*pi/180)
arrow (x0, y0, dx, dy,
width=width,
edgecolor=color,
facecolor=color,
antialiased=True,
head_width=5*width,
head_length=7.5*width,
**kwargs)
class ArrowRight(Arrow2):
def __init__(self, x0, y0, length, **kwargs):
Arrow2.__init__(self, x0, y0, length, angle=0.0, **kwargs)
class ArrowLeft(Arrow2):
def __init__(self, x0, y0, length, **kwargs):
Arrow2.__init__(self, x0, y0, length, angle=180.0, **kwargs)
class ArrowUp(Arrow2):
def __init__(self, x0, y0, length, **kwargs):
Arrow2.__init__(self, x0, y0, length, angle=90.0, **kwargs)
class ArrowDown(Arrow2):
def __init__(self, x0, y0, length, **kwargs):
Arrow2.__init__(self, x0, y0, length, angle=270.0, **kwargs)
if __name__ == "__main__":
main()
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(4)
-
aws
(8)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(9)
-
conkeror
(2)
-
cygwin
(18)
-
datastructures
(15)
-
datetime
(3)
-
dell
(3)
-
django
(39)
-
emacs
(20)
-
files_directories
(10)
-
install_setup
(7)
-
javascript
(3)
-
keyboard
(6)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
preferences
(8)
-
processes
(3)
-
pyqt
(18)
-
python
(122)
-
ratpoison
(3)
-
regexes
(5)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(13)
-
ssh
(7)
-
subversion
(6)
-
twisted
(6)
-
ubuntu
(60)
-
urxvt
(5)
-
vxworks
(25)
-
webservices
(4)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan