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

摘要:
首先,确认有一个Python文件(filename.py),Python文件_名称.py尽管控制台可以运行Python,但//www.jetbrains.com/pychar/download/#section=Linux官方网站上有两个版本的专业版和社区版,然后按照解压缩的指令文件执行安装命令以成功安装(一些计算机可能会由于配置原因报告错误)。

    在研究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界面如下图。

Python学习(一) —— matplotlib绘制三维轨迹图第1张

    二. matplotlib绘制三维轨迹

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

    2.1. 绘制基本三维曲线

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

# import necessary module
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy 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
print first_2000
print second_2000
print third_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()

Python学习(一) —— matplotlib绘制三维轨迹图第2张

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

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

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

    代码和输出结果如下:

# import necessary module
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy 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()

Python学习(一) —— matplotlib绘制三维轨迹图第3张

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

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

    代码和输出结果如下:

# import necessary module
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy 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()

Python学习(一) —— matplotlib绘制三维轨迹图第4张

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

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

免责声明:文章转载自《Python学习(一) —— matplotlib绘制三维轨迹图》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇js-ajax方法详解以及封装【python】threadpool的内存占用问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

创建podSpec,使用pod管理第三方库

 提要:   podfile文件会先读取.podspec文件,根据.podspec文件的指向来下载第三方库到项目中。   本文先通过一、二、三这三个步骤讲解了如何建立一个"podspec文件在本地.cocoaPod库,第三方库在远程机器"的例子。   后文中的第四项,讲解了"podspec文件在本地项目中,第三方库在远程机器"的设置方法;最后讲了"pods...

Hadoop HDFS命令行操作

1、列出所有命令 hdfs dfs -help 2、查看某个一个命令的详细 hdfs dfs -help -put 3、ls 查看HDFS系统中文件和目录,例如查看根目录 hdfs dfs -ls /  列出所有目录和文件 hdfs dfs -ls -R /  4、put 将本地文件上传道HDFS系统中 hdfs dfs -put test.txt...

showdoc升级问题,showdoc错误日志

showdoc自带错误日志。目录位于网站根目录的server/Application/Runtime/Logs/Api目录下,如果没有任何内容需要添加可写权限。 showdoc升级后,建议把MySQL改成Sqlite,这里我们没改。后期出现了一系列问题,观察这个位置的日志就能解决。 建议升级的用户看一下网站根目录下server/Application/Ho...

如何打开mo文件并修改 PoEdit

  mo文件是被编译了的文件,一般在汉化的时候会用到。 比如我想修改phpmyadmin 的界面信息,就需要修改phpmyadmin.mo的文件内容。 可是用编辑器editplus或者Sublime 2打开它,都是乱码。 原来,这个文件不能直接打开。 搜索了一下,网上的信息。 发现通过poedit软件,可以先将mo转化为po格式的文件,然后poedit就...

windows下的shellcode剖析浅谈[转自看雪]

标 题: 【原创】windows下的shellcode剖析浅谈作 者:snowdbg时 间: 2009-10-06,11:12链 接: http://bbs.pediy.com/showthread.php?t=99007  今天是中秋节,正好我的文章在今天基本完成,作为中秋礼物送给大家,由于本人水平有限希望大家多多批评指正!学习了好些日子了,思路总是乱...

crnn转换数据集

在做crnn实验的时候数据的格式是一张图片对应一个标签,比如说 图片名称 1.jpg 内容是 你好呀 那么你的标签就应该是 1.txt 在网上找了一个数据集 https://github.com/YCG09/chinese_ocr 数据集下载地址 数据集:https://pan.baidu.com/s/1QkI7kjah8SPHwOQ40rS1Pw (密码...