栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python画轨迹图

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python画轨迹图

 

import cmath
import re
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from matplotlib.animation import FuncAnimation
import os

azimuth_list = []
distance_list = []

# x_axis = []
# y_axis = []

aoa_loc_data = []

index = 0
curve = 0

output_gif = "output\gif\"
output_csv = "output\csv\"
output_png = "output\png\"
def create_csv(file_input, line, data):
    global azimuth
    global distance

    with open(file_input, 'w') as file_ob:
        while line:
            line = data.readline()

            tmp_distance_re = re.search(r'Distance d+', line)
            tmp_azimuth_re = re.search(r'AoA azimuth -?d+', line)
            if tmp_distance_re and tmp_azimuth_re:
                distance = int(tmp_distance_re.group(0).replace('Distance ', ''))
                distance /= 100
                azimuth = int(tmp_azimuth_re.group(0).replace('AoA azimuth ', ''))
                #azimuth = 0
                file_ob.write('%.02f,%dn' % (distance, azimuth))
                #
                # print('原始:%.02fm %d' % (distance, azimuth))
                # azimuth = 0 - azimuth;
                # print('镜像:%.02fm %d' % (distance, azimuth))
                # #azimuth = azimuth if (azimuth >= 0) else (azimuth + 360)
                # azimuth = azimuth * np.pi / 180.
                # print("弧度:%.02fm %f" % (distance, azimuth))
                # file_ob.write('%.02f,%.02fn' % (distance, azimuth))



def load_file(file_name):
    _log_file_name = file_name + '.log'
    _csv_filename = os.path.join(output_csv, file_name)
    _csv_filename = _csv_filename + '.csv'
    data = open(_log_file_name)
    line = data.readline()
    create_csv(_csv_filename, line, data)
    data.close()


def load_data(file_name):
    global aoa_loc_data
    #csv_filename = file_name + '.csv'
    _csv_filename = os.path.join(output_csv, file_name)
    _csv_filename = _csv_filename + '.csv'

    load_file(file_name)
    aoa_loc_data = pd.read_csv(_csv_filename, header=None)

    print(len(aoa_loc_data[0]))
    print(len(aoa_loc_data[1]))

def update_graph(step):
    #print(azimuth_list)
    #print(distance_list)
    global index
    global curve
    #print(aoa_loc_data[1])

    if index == len(aoa_loc_data[0]):
        return

    azimuth = aoa_loc_data[1][index]
    azimuth = azimuth if (azimuth >= 0) else (azimuth + 360)
    azimuth = azimuth * np.pi / 180.

    distance = aoa_loc_data[0][index]

    azimuth_list.append(azimuth)
    distance_list.append(distance)
    curve.set_data(azimuth_list, distance_list)
    index += 1
    print("azimuth:%f, distance:%f" %(azimuth, distance))

def save_ani_fig(file_name):
    global curve
    global index
    global azimuth_list
    global distance_list
    global aoa_loc_data
    #init
    azimuth_list = []
    distance_list = []
    index = 0
    aoa_loc_data = []

    load_data(file_name)
    fig = plt.figure(file_name)
    pic = fig.add_subplot(111, projection='polar')
    pic.set_title(file_name, va='bottom')
    pic.set_rmax(8)
    pic.set_rticks(np.arange(1, 8, 1))
    pic.set_rlabel_position(0)
    # pic.annotate('a polar annotation',
    #             xy=(0, 0),  # theta, radius
    #             xytext=(0.05, 0.05),    # fraction, fraction
    #             textcoords='figure fraction',
    #             arrowprops=dict(facecolor='black', shrink=0.05),
    #             horizontalalignment='left',
    #             verticalalignment='bottom',
    #             )
    curve, =  pic.plot(azimuth_list, distance_list, linestyle='', marker='*', lw=0.5)
    pic.grid(True)
    ani = FuncAnimation(fig, update_graph, frames=len(aoa_loc_data[0]), interval=20, repeat=False)
    path = os.getcwd()
    #ani_name = path + file_name + '.gif'
    ani_name = os.path.join(path, output_gif, file_name)
    ani_name = ani_name + '.gif'
    ani.save(ani_name, writer='Pillow',fps=40)
    png_name = os.path.join(path, output_png, file_name)
    plt.savefig(png_name)
    #plt.show()

def find_all_file(base):
    for root, dirs, files in os.walk(base):
        for f in files:
            #print(f)
            yield f

if __name__ == '__main__':
    #makedirs() is recursive directory creation function
    os.makedirs(output_png)
    os.makedirs(output_gif)
    os.makedirs(output_csv)
    path = os.getcwd()
    print(path)
    #find_all_file(path)
    for i in find_all_file(path):
        if i.endswith('.log'):
            print(i)
            file_name = os.path.splitext(i)[0]
            save_ani_fig(file_name)
            # path = os.getcwd()
            # # path += "\dst\"
            # path = os.path.join(path, "dst", file_name)
            # #ani_name = path + file_name + '.gif'
            # ani_name = path + '.gif'
            # print(ani_name)












import cmath
import re
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math
from matplotlib.animation import FuncAnimation

distance = 0
azimuth = 0

x_axis = []
y_axis = []

aoa_loc_data = []

index = 0

def create_csv(file_input, line, data):
    global azimuth
    global distance
    ret = 0
    with open(file_input, 'w') as file_ob:
        while line:
            line = data.readline()

            tmp_distance_re = re.search(r'Distance d+', line)
            tmp_azimuth_re = re.search(r'AoA azimuth -?d+', line)
            if tmp_distance_re and tmp_azimuth_re:
                distance = int(tmp_distance_re.group(0).replace('Distance ', ''))
                distance /= 100
                azimuth = int(tmp_azimuth_re.group(0).replace('AoA azimuth ', ''))
                #azimuth = 0
                #file_ob.write('%.02f,%dn' % (distance, azimuth))
                #
                print('原始:%.02fm %d' % (distance, azimuth))
                azimuth = 0 - azimuth;
                print('镜像:%.02fm %d' % (distance, azimuth))
                #azimuth = azimuth if (azimuth >= 0) else (azimuth + 360)
                azimuth = azimuth * np.pi / 180.
                print("弧度:%.02fm %f" % (distance, azimuth))
                file_ob.write('%.02f,%.02fn' % (distance, azimuth))
                ret = 2
            else:
                ret = 0

def load_file(file_string):
    global filename
    log_file_name = file_string + '.log'
    filename = log_file_name
    filename = file_string + '.csv'
    data = open(log_file_name)
    line = data.readline()
    create_csv(filename, line, data)
    data.close()

def load_data(file_name):
    global aoa_loc_data
    load_file(file_name)
    aoa_loc_data = pd.read_csv(filename, header=None)

    print(len(aoa_loc_data[0]))
    print(len(aoa_loc_data[1]))

    # ax = fig.add_subplot(111, projection='polar')
    # ax.set_rmax(3)
    # # ax.set_rticks(np.arange(1, 10, 1))
    # ax.set_rticks(np.arange(1, 8, 1))
    # ax.set_rlabel_position(0)
    #
    # curve, = ax.plot(aoa_data[0], aoa_data[1], linestyle='', marker='*', lw=0.5)
    # ax.grid(True)


    # for i in range(len(aoa_data[0])):
    #     #print(i)
    #     #print(aoa_data[0][i])
    #     x = aoa_data[0][i] * math.cos(aoa_data[1][i])
    #     y = aoa_data[0][i] * math.sin(aoa_data[1][i])
    #
    #     # x = aoa_data[0][i] * math.cos(math.fabs(aoa_data[1][i]))
    #     # y = aoa_data[0][i] * math.sin(math.fabs(aoa_data[1][i]))
    #
    #     x_axis.append(x)
    #     y_axis.append(y)
    #     print("Distance:%f, AoA:%f, x:%f, y:%f" %(aoa_data[0][i], aoa_data[1][i], x, y))
    # #plt.plot(x_axis, y_axis, 'o', label = "aoa_loc cartesian")
    # plt.scatter(x_axis, y_axis)

def update_graph(step):
    global index
    global pic_plot
    print("index:%d, len:%d" %(index, len(aoa_loc_data[0])))
    if index < len(aoa_loc_data[0]):
        x = aoa_loc_data[0][index] * math.cos(aoa_loc_data[1][index])
        y = aoa_loc_data[0][index] * math.sin(aoa_loc_data[1][index])
        index += 1
        x_axis.append(x)
        y_axis.append(y)
    pic_plot.set_data(x_axis, y_axis)

log_file_name = 'yleft_02'

# gif_name_dict = {"xleft_01": "anchor_left_side_row_scan_01",
#                  "xleft_02": "anchor_left_side_row_scan_02",
#                  "xright_01": "anchor_right_side_row_scan_01",
#                  "xright_02": "anchor_right_side_row_scan_02",
#                  "yleft_01": "anchor_left_side_column_scan_01",
#                  "yleft_02": "anchor_left_side_column_scan_02",
#                  "yright_01": "anchor_right_side_column_scan_01",
#                  "yright_02": "anchor_right_side_column_scan_02"}

gif_name_dict = {"xleft_01": "anchor_right_side_row_scan_01",
                 "xleft_02": "anchor_right_side_row_scan_02",
                 "xright_01": "anchor_left_side_row_scan_01",
                 "xright_02": "anchor_left_side_row_scan_02",
                 "yleft_01": "anchor_right_side_column_scan_01",
                 "yleft_02": "anchor_right_side_column_scan_02",
                 "yright_01": "anchor_left_side_column_scan_01",
                 "yright_02": "anchor_left_side_column_scan_02"}

gif_name = gif_name_dict[log_file_name]

fig = plt.figure("行走轨迹")
pic = fig.add_subplot(111)
pic.set_xlim(-8, 8)
pic.set_ylim(-8, 8)
#pic.grid(True)
plt.title(gif_name)
circle_rad = 15
#plt.annotate('anchor(0,0)', xy=(0, 0), xytext=(0, 0),arrowprops=dict(facecolor='black', shrink=0.05))
plt.annotate("anchor:(%s,%s)" % (0,0), xy=(-2,0), xytext=(-20, 10), textcoords='offset points')
#plt.annotate('anchor', xy=(0,0), xytext=(20,20), textcoords='offset points', color='b', size='large', arrowprops=dict(arrowstyle='simple, tail_width=0.3, head_width=0.8, head_length=0.8', facecolor='b', shrinkB=circle_rad * 1.2))
#plt.annotate('anchor(0,0)', xy=(0,0), xytext=(-2,0),arrowprops={'arrowstyle': '->', 'lw': 4, 'color': 'blue'},va='center')

pic.plot(0, 0, linestyle='', marker='o', lw=0.5, color='r')

pic_plot, = pic.plot(x_axis, y_axis, linestyle='', marker='.', lw=0.5, color='g')
#pic_plot = pic.plot(x_axis, y_axis, 'o', color='r')

load_data(log_file_name)
anim_name = gif_name + '.gif'
anim = FuncAnimation(fig, update_graph, frames=len(aoa_loc_data[0]), interval=10, repeat=False)
#anim.save(anim_name, writer='Pillow',fps=40)
plt.show()




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/843996.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号