python绘制三维轨迹_Python学习(一) —— matplotlib绘制三维轨迹图

news/2024/7/5 4:59:30

在研究SLAM时常常需要对其输出的位姿进行复现以检测算法效果,在ubuntu系统中使用Python可以很好的完成相关的工作。

一. Ubuntu下Python的使用

在Ubuntu下使用Python有两种方法,一种是直接在控制台中运行Python文件,一种是下载IDE编辑并运行Python文件。

在控制台中使用Python方法如下:

首先确认有Python文件(filename.py),然后打开控制台进入文件当前目录,并输入以下内容就可以运行了。

python file_name.py

虽然控制台可以运行Python,不过由于不能调试等问题仍然比较推荐使用IDE。

目前使用的Python IDE为PyCharm,官方下载地址为https://www.jetbrains.com/pycharm/download/#section=linux

官网中提供professional和community两种版本,因为community版本免费大家可以直接下载使用。下载好后直接放到安装目录中解压,然后跟着解压后的说明文件执行安装命令即可安装成功(部分电脑由于配置原因可能会报错,网上有很多讲解配置环境安装博客,大家可以参考)。安装成功后,PyCharm界面如下图。

二. matplotlib绘制三维轨迹

Matplotlib是Python的一个绘图库,想面将讲解如何使用这个库来绘制三维线段,以此检测SLAM算法的输出结果(电脑配置Python 2.7)。

2.1. 绘制基本三维曲线

首先给出完整代码,以及输出结果。

#import necessary module

from mpl_toolkits.mplot3d importaxes3dimportmatplotlib.pyplot as pltimportnumpy as np#load data from file#you can replace this using with open

data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt")

first_2000= data1[:, 3]

second_2000= data1[:, 7]

third_2000= data1[:, 11]#print to check data

printfirst_2000printsecond_2000printthird_2000#new a figure and set it into 3d

fig =plt.figure()

ax= fig.gca(projection='3d')#set figure information

ax.set_title("3D_Curve")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure = ax.plot(first_2000, second_2000, third_2000, c='r')

plt.show()

这段代码非常简单,而且相关的注释也很完善,因此只简要说明几个需要注意的地方。第一个需要注意的是读取文件中数据比较推荐用with open 然后逐行读取;第二点是在新建图像时一定别忘了添加这段代码,这是输出图像设定为3D的关键。

ax = fig.gca(projection='3d')

2.2. 同一张图中绘制多个三维曲线

代码和输出结果如下:

#import necessary module

from mpl_toolkits.mplot3d importaxes3dimportmatplotlib.pyplot as pltimportnumpy as np#load data from file#you replace this using with open

data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt")

first_2000= data1[:, 3]

second_2000= data1[:, 7]

third_2000= data1[:, 11]

data2= np.loadtxt("./stereo/CameraTrajectoryNew1500.txt")

first_1000= data2[:, 3]

second_1000= data2[:, 7]

third_1000= data2[:, 11]#new a figure and set it into 3d

fig =plt.figure()

ax= fig.gca(projection='3d')#set figure information

ax.set_title("3D_Curve")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure1 = ax.plot(first_2000, second_2000, third_2000, c='r')

figure2= ax.plot(first_1000, second_1000, third_1000, c='b')

plt.show()

实现这个功能只需要在之前代码中加入读取新数据的相关代码,以及在画图时多生成一个图即可,也是非常的简单。

2.3. 将区域划分后绘制三维图像

代码和输出结果如下:

#import necessary module

from mpl_toolkits.mplot3d importaxes3dimportmatplotlib.pyplot as pltimportnumpy as np#load data from file#you replace this using with open

data1 = np.loadtxt("./stereo/CameraTrajectoryNew2000.txt")

first_2000= data1[:, 3]

second_2000= data1[:, 7]

third_2000= data1[:, 11]

data2= np.loadtxt("./stereo/CameraTrajectoryNew1500.txt")

first_1500= data2[:, 3]

second_1500= data2[:, 7]

third_1500= data2[:, 11]#new a figure and set it into 3d

fig =plt.figure()############# first subplot ############

ax = fig.add_subplot(2, 2, 1, projection='3d')

ax.set_title("3D_Curve1")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure1 = ax.plot(first_2000, second_2000, third_2000, c='r')

figure2= ax.plot(first_1500, second_1500, third_1500, c='b')############# second subplot ############

ax = fig.add_subplot(2, 2, 2, projection='3d')#set figure information

ax.set_title("3D_Curve2")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure1 = ax.plot(first_2000, second_2000, third_2000, c='r')

figure2= ax.plot(first_1500, second_1500, third_1500, c='b')############# third subplot ############

ax = fig.add_subplot(2, 2, 3, projection='3d')#set figure information

ax.set_title("3D_Curve3")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure1 = ax.plot(first_2000, second_2000, third_2000, c='r')

figure2= ax.plot(first_1500, second_1500, third_1500, c='b')############# fourth subplot ############

ax = fig.add_subplot(2, 2, 4, projection='3d')#set figure information

ax.set_title("3D_Curve4")

ax.set_xlabel("x")

ax.set_ylabel("y")

ax.set_zlabel("z")#draw the figure, the color is r = read

figure1 = ax.plot(first_2000, second_2000, third_2000, c='r')

figure2= ax.plot(first_1500, second_1500, third_1500, c='b')

plt.show()

主要需要解释下面这行代码:

ax = fig.add_subplot(2, 2, 1, projection='3d')

这行代码主要是说,将当前空间拆分建立新的子图,子图个数为四,按照2x2矩阵排列方式进行,当前子图为四个子图中的第一个,且为3D模式。

以上就是就是如何使用matplotlib绘制三位曲线。总的来说比较简单,没有什么难度,Python的确是一个非常好用的编程语言。更多关于matplotlib的使用方法大家可以参考他们的官方网站,里面有相关的tutorial以及examples。

Matplotlib Introduction:http://matplotlib.org/index.html

Matplotlib Samples:http://matplotlib.org/examples/index.html


http://lihuaxi.xjx100.cn/news/271381.html

相关文章

百度重磅发布云手机:低配置也可玩大型游戏 21

又一个科技巨头发力云游戏。4月15日,百度举行"云手机"线上直播会,发布基于自主研发的ARM服务器的百度"云手机"产品,让用户摆脱硬件的制约,中低端设备也能流畅运行大型游戏和应用。百度"云手机"可以…

linux 循环shell脚本,shell脚本的使用---for循环

shell脚本的循环:重复执行命令1.for循环语法for 变量名称 in 变量值列表do命令donefor根据变量值列表中的内容,重复执行命令,直到变量值列中的所有内容都取值完后结束。取值列表的类型:可以是特定文本文件,命令生成列表…

kaggle图像分割实战要点和技巧总结

点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达作者参加了39个Kaggle比赛,总结了非常多的技巧和经验,现在全部分享给大家。想象一下,如果你能得到所有的tips和tricks,你需…

一次Dubbo拥堵的分析

点击上方“方志朋”,选择“设为星标”回复”666“获取新整理的面试文章作者:nxlherohttps://blog.51cto.com/nxlhero/2515849文章内容结构第一部分介绍生产上出现Dubbo服务拥堵的情况,以及Dubbo官方对于单个长连接的使用建议。第二部分介绍Du…

键盘遮挡

在群里有小伙伴用第三方来解决键盘遮挡, 在网上搜了一下, 自己做了一个demo //开始编辑输入框的时候,软键盘出现,执行此事件 -(void)textFieldDidBeginEditing:(UITextField *)textField {int offset self.view.frame.size.height - textFie…

最新!2020中国高校毕业生薪资报告出炉

点击上方“视学算法”,选择加"星标"或“置顶”重磅干货,第一时间送达本文来源:中国薪酬网近日,中国薪酬网发布了《2020中国高校毕业生薪酬指数排名》。此次排名按照工资水平、就业率、人才成长率、薪酬增长率四个要素&a…

mongodb 结果二次调用_mongodb慢查询记录

在 MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是MongoDatabase Profiler.不仅有,而且还有一些比MySQL的Slow QueryLog更详细的信息。它就是我们这篇文章的主题。开启…

Confluence 6 配置文件和key

2019独角兽企业重金招聘Python工程师标准>>> 找到配置文件 缓存的配置文件是存储在 <confluence-home>/shared-home/config/cache-settings-overrides.properties 中的、 有关 Confluence 数据中心&#xff08;集群&#xff09;中缓存的配置&#xff0c;你可以…