Drawing Arrow in (x,y) coordinate in Python

computational-geometry, matplotlib, python-2.7

Solution

Your designated angles follow map conventions, whereas the Cartesian conventions have (+x, +y, -x, -y) as (0, 90, 180, 270) respectively. They will also take radians. To convert your angles:

import math
cartesianAngleRadians = (450-mapAngleDegrees)*math.pi/180.0

Here's source code that draws tick marks according to your provided x,y points.

import numpy as np
import scipy as sp
import pylab as pl
import math

x = np.array([ 2, 4, 8, 10, 12, 14, 16])
y = np.array([ 5, 10, 15, 20, 25, 30, 35])
angles = np.array([45,275,190,100,280,18,45]) 

def draw_line(x,y,angle,length):
  cartesianAngleRadians = (450-angle)*math.pi/180.0
  terminus_x = x + length * math.cos(cartesianAngleRadians)
  terminus_y = y + length * math.sin(cartesianAngleRadians)
  pl.plot([x, terminus_x],[y,terminus_y])
  print [x, terminus_x],[y,terminus_y]


pl.axis('equal')
pl.axis([-5,20,-5,40])
for i in range(0,len(x)):
  print x[i],y[i],angles[i]
  draw_line(x[i],y[i],angles[i],1)

pl.show()

Problem

I have encountered some problem while I was drawing a direction of arrow. I have point (x,y) coordinates and angle of them. What I want to do is that to draw arrow according to the given angle (just to show the point direction as an arrow in each point coordinate). Here, we should assume coordinates of '+x', '+y', '-x ', '-y' are 90, 0, 270, 180 degrees, respectively. I am a bit unfamiliar with Python drawing tools. I am still not sure to draw directional point (arrow based on angle) whether I use pylab or some other modules or.. still not sure at all. I put the following codes as a sample to give better description: ``` # Inputs: x = np.array([ 2, 4, 8, 10, 12, 14, 16]) y = np.array([ 5, 10, 15, 20, 25, 30, 35]) angles = np.array([45,275,190,100,280,18,45]) import numpy as np import scipy as sp import pylab as pl def draw_line(x,y,angle): # First, draw (x,y) coordinate ??? # Second, according to the angle indicate the direction as an arrow ??? ```

Original source