繁体   English   中英

在 python 中绘制坐标与方向

[英]Plotting coordinates with direction in python

我有一个 dataframe,它具有天线坐标和天线指向波束的角度。 有2个坐标相同但角度不同的天线。 所以我想用三角形表示这些,箭头的头表示角度。

df = 
    Antenna   Latitude    Longitude   Angle
0   A           19.25     7.21         170
1   B           19.25     7.21         350
2   C           19.29     7.29         240
3   D           19.29     7.29         300

我尝试了以下内容:

fig, ax1 = plt.subplots() 
  
ax1.set_xlabel('LAT') 
ax1.set_ylabel('LON')
ax1.scatter(19.25, 7.21,label = 'A and B',markers='^',200 )
ax1.scatter(19.29, 7.29,label = 'C and D',marker='^'s=200 )
plt.legend()

这给了我下面的图片

在此处输入图像描述

现在我想用不同的三角形来表示每个天线,三角形的头应该指向这个角度。 有没有办法做到这一点?

我不认为三角形是一个好的方向指示器,因为有三个角度。 所以在这里我使用箭头(箭袋)来指示方向。

import pandas as pd
from io import StringIO
import numpy as np
import matplotlib.pyplot as plt
inf='''
Antenna   Latitude    Longitude   Angle
A           19.25     7.21         170
B           19.25     7.21         350
C           19.29     7.29         240
D           19.29     7.29         300
'''
df = pd.read_csv(StringIO(inf),sep="\s+")

# u = ws * cos(θ)
# v = ws * sin(θ)
# http://colaweb.gmu.edu/dev/clim301/lectures/wind/wind-uv

df['u'] = np.cos(np.deg2rad(df['Angle']))
df['v'] = np.sin(np.deg2rad(df['Angle']))


fig, ax1 = plt.subplots() 
  
ax1.set_xlabel('LAT') 
ax1.set_ylabel('LON')
colors = ['red','blue']
for ii,item in enumerate(df.groupby(['Latitude','Longitude'])):
    ll, df1 = item
    ax1.quiver(df1.Longitude, df1.Latitude,df1.u,df1.v ,color = colors[ii])

在此处输入图像描述

import numpy as np
import matplotlib.pyplot as plt

# Here your data, in a different data structure    
data = {(7.21,19.25):dict(A=170,B=350),
        (7.29,19.29):dict(C=240,D=300)}

# Let's plot a point for each location, zorder is larger because
# we want the point to appear over the arrows
for loc in data:
    plt.scatter(*loc, zorder=3, label="dummy string")

# place the legend in the axes, get an handle to it
legend = plt.legend()
# then, using the handle, change the labels
for loc, text in zip(data, legend.get_texts()):
    label = ', '.join(antenna for antenna in data[loc])
    text.set_text(label)

# eventually, for each antenna, draw an arrow directed like the angle 
# maybe 200 pixels is too much?   
for loc, antennas in data.items():
    for antenna, angle in antennas.items():
        angle_r = np.radians(angle)
        s, c = np.sin(angle_r), np.cos(angle_r)
        xt, yt = 200*c, 200*s # in pixels
        plt.annotate(antenna, xy=loc, xytext=(xt, yt),
                     textcoords='offset pixels',
                     arrowprops=dict(arrowstyle='<-'))
# something is possibly outside of the figure...
plt.tight_layout()
plt.show()

在此处输入图像描述

PS 在 170⁰ 和 350⁰ 光束之间有一个小的偏差,可能是由于舍入问题 - 我正在考虑,但我还没有找到解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM