在Python中的(x,y)坐标中绘制箭头

Spi*_*der 4 matplotlib computational-geometry python-2.7

我在绘制箭头方向时遇到了一些问题.我有点(x,y)坐标和它们的角度.我想要做的是根据给定的角度绘制箭头(只是为了在每个点坐标中将点方向显示为箭头).这里,我们应该假设'+ x','+ y',' - x',' - y'的坐标分别是90度,0度,270度,180度.

我对Python绘图工具有点不熟悉.我仍然不确定绘制方向点(基于角度的箭头)我是否使用pylab或其他模块或..仍然不确定.我将以下代码作为示例进行了更好的描述:

 # 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 ???
Run Code Online (Sandbox Code Playgroud)

Ben*_*ier 9

你可以用箭头绘制matplotlib.pyplot.arrow(x, y, dx, dy, hold=None, **kwargs).您似乎遇到困难的部分是定义偏移dx并dy给出角度和箭头长度r.对于angle以弧度为单位的极坐标

dx = r*cos(angle)
dy = r*sin(angle)
Run Code Online (Sandbox Code Playgroud)

这样你的draw_line功能就变成了

def draw_line(x, y, angle):
    r = 1  # or whatever fits you
    arrow(x, y, r*cos(angle), r*sin(angle))
Run Code Online (Sandbox Code Playgroud)

这将绘制一个箭头,从长度(x,y)方向开始.angle1


Mar*_*ing 3

您指定的角度遵循地图约定,而笛卡尔约定将 (+x, +y, -x, -y) 分别表示为 (0, 90, 180, 270)。它们也将采用弧度。要转换角度:

import math
cartesianAngleRadians = (450-mapAngleDegrees)*math.pi/180.0
Run Code Online (Sandbox Code Playgroud)

这是根据您提供的 x,y 点绘制刻度线的源代码。

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()
Run Code Online (Sandbox Code Playgroud)