PYTHON画地图

该代码示例使用Python的matplotlib库读取地图、重定位和ODO数据,然后在图表上绘制数据点并添加标注。每个数据点的位置、角度信息通过`OutputCoordinate`函数处理,并在鼠标移动时动态显示相关信息。最后,程序展示带有图例和网格的图表。
摘要由CSDN通过智能技术生成
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

xx = []
yy = []
th = []

def OutputCoordinate(x,y,th):
    strlist="x=%.2f,y=%.2f,th=%.2f"%(x,y,th)
    # PointTipText=plt.text(x,y*1.02,strlist, ha='center', va='bottom', fontsize=10)
    # plt.text(x,y*1.02,str('/'), ha='center', va='bottom', fontsize=10.5)
    # plt.text(x+0.07,y*1.02,"y=%.2f"%y, ha='center', va='bottom', fontsize=10)
    # print(x,y)
    # print(PointTipText)
    return strlist

def mapdataDispose():
    Path = "D:\RTK_DATA_DISPOSE\RTKtest\\mapdata.txt"

    infile = open(Path, 'rb')

    numCount=0
    line=infile.readline()
    while line:
        a=line.split()
        x=float(a[0])
        y=float(a[1])
        angle=float(a[2])
    # if numCount%2==0:
        xx.append(x)
        yy.append(y)
        th.append(angle)
        OutputCoordinate(x,y,angle)
        numCount=numCount+1
        line=infile.readline()

    # point,=plt.plot(xx,yy,marker = "+",markersize=7)#marker设置标记形状 markersize设置标记大小
    # plt.xlim(1,100)#限制取值范围
    # plt.ylim(1,100)
    # plt.xticks(np.arange(-10,10,1))#设置取值范围和间隔
    # plt.yticks(np.arange(-10,10,1))
    # plt.legend()  # 显示图例
    # plt.grid()  # 生成网格
    # plt.show()
    infile.close()

def RelocationdataDispose():
    Path = "D:\RTK_DATA_DISPOSE\RTKtest\\relocation.txt"

    infile = open(Path, 'rb')

    numCount=0
    line=infile.readline()
    while line:
        a=line.split()
        # plt.plot(gpsData[numCount,1],gpsData[numCount,2],'ro')
        x=float(a[1])
        y=float(a[2])
        angle=float(a[0])
        # if numCount%2==0:
        xx.append(x)
        yy.append(y)
        th.append(angle)
        OutputCoordinate(x,y,angle)
        numCount=numCount+1
        line=infile.readline()

    # point,=plt.plot(xx,yy,marker = "+",markersize=7)#marker设置标记形状 markersize设置标记大小
    # plt.xlim(1,100)#限制取值范围
    # plt.ylim(1,100)
    # plt.xticks(np.arange(-10,10,1))#设置取值范围和间隔
    # plt.yticks(np.arange(-10,10,1))
    # plt.legend()  # 显示图例
    # plt.grid()  # 生成网格
    # plt.show()
    infile.close()

def ODOdataDispose():
    Path = "D:\RTK_DATA_DISPOSE\RTKtest\\mapdata.txt"

    infile = open(Path, 'rb')

    numCount=0
    line=infile.readline()
    while line:
        a=line.split()
        # plt.plot(gpsData[numCount,1],gpsData[numCount,2],'ro')
        x=float(a[2])
        y=float(a[3])
        angle=float(a[5])
        # if numCount%2==0:
        xx.append(x)
        yy.append(y)
        th.append(angle)
        OutputCoordinate(x,y,angle)
        numCount=numCount+1
        line=infile.readline()

    # point,=plt.plot(xx,yy,marker = "+",markersize=7)#marker设置标记形状 markersize设置标记大小
    # plt.xlim(1,100)#限制取值范围
    # plt.ylim(1,100)
    # plt.xticks(np.arange(-10,10,1))#设置取值范围和间隔
    # plt.yticks(np.arange(-10,10,1))
    # plt.legend()  # 显示图例
    # plt.grid()  # 生成网格
    # plt.show()
    infile.close()

# 定义鼠标响应函数
def on_move(event):
    visibility_changed = False
    for point, annotation in po_annotation1:
        should_be_visible = (point.contains(event)[0] == True)

        if should_be_visible != annotation.get_visible():
            visibility_changed = True
            annotation.set_visible(should_be_visible)

    if visibility_changed:
        # fig.canvas.draw_idle()#重新绘制整个图表 
        plt.plot(xx,yy,'b',marker = "+",markersize=7)#marker设置标记形状 markersize设置标记大小
        plt.draw()


mapdataDispose();
#RelocationdataDispose();

# ODOdataDispose()

fig,ax = plt.subplots()
# fig.set_size_inches(24, 12)
# po_annotation1 为 ‘买’ 时需要显示的标注信息
# 该 list 内部存放多个子 list,每个子 list 为 [标注点的坐标, 标注]
po_annotation1 = []
for i in range(len(xx)):
    # 标注点的坐标
    point_x = xx[i]
    point_y = yy[i]
    point_th = th[i]
    point, = plt.plot(point_x, point_y,'b.:',markersize=7,linewidth=1)
    # print(point)
    # 标注框偏移量
    offset1 = 20
    offset2 = 20
    # 标注框
    bbox1 = dict(boxstyle="round", fc='lightgreen', alpha=0.6)
    # 标注箭头
    arrowprops1 = dict(arrowstyle="->", connectionstyle="arc3,rad=0.")
    # 标注
    annotation = plt.annotate((i,OutputCoordinate(point_x,point_y,point_th)), xy=(point_x,point_y), xytext=(-offset1, offset2), textcoords='offset points',
                              bbox=bbox1, arrowprops=arrowprops1, size=15)
    # 默认鼠标未指向时不显示标注信息
    annotation.set_visible(False)
    po_annotation1.append([point, annotation])

# 鼠标移动事件
on_move_id = fig.canvas.mpl_connect('motion_notify_event', on_move)
plt.legend()  # 显示图例
plt.grid()  # 生成网格
plt.show()

用python画地图数据,并输出图形

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值