一、相关的程序码仓地址
Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。
相关参考资料
Matplotlib 中文:https://www.matplotlib.org.cn/
一些应用实例和vs配置:https://mangoroom.cn/cpp/call-matplotlib-on-cpp.html
matplotlib-cpp接口工程:https://github.com/lava/matplotlib-cpp
Matplotlib for C++使用手册:https://matplotlib-cpp.readthedocs.io/en/latest/
源程序GitHub仓地址:https://github.com/lava/matplotlib-cpp
二、测试
文件夹结构
下载源码,并将头文件matplotlibcpp.h
放到自己的工程文件中,或者在编译文件中链接该头文件。
测试使用的工程文件目录结构如下
test.cpp
测试程序文件编写
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
plt::plot({1,3,2,4});
plt::show();
}
CMakeLists.txt
文件编写
# 声明要求的 cmake 最低版本
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_STANDARD 11)
project(TEST)
# 查找python库版本
find_package(PythonLibs 2.7)
# 指定头文件路径
set(PYTHON2.7_INLCUDE_DIRS "/usr/include/python2.7")
# 添加头文件到工程
include_directories(
${PYTHON2.7_INLCUDE_DIRS}
)
#这样配置可以使得matplotlib.h中找到python.h文件,通常这样设置就可以。
# 添加一个可执行程序
add_executable(test test.cpp)
# 添加相关库文件链接到工程
target_include_directories(test PRIVATE ${PYTHON2_INCLUDE_DIRS})
target_link_libraries(test ${PYTHON_LIBRARIES})
测试效果如下
进行相关的编译及程序运行
新建build
编译文件夹,并进行编译运行
mkdir build
cd build
cmkae ..
make
./test
三、常用程序模块
分布式画布例程
/*--------包含的头文件-----------*/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
#include "matplotlibcpp.h"
/*---------程序的主体例程----------*/
std::vector<double> time;
std::vector<double> roll;
std::vector<double> pitch;
std::vector<double> yaw;
std::vector<double> vx;
std::vector<double> vy;
std::vector<double> vz;
std::vector<double> x;
std::vector<double> y;
std::vector<double> z;
/*************************/
/*该部分存入相关容器的数据*/
/************************/
matplotlibcpp::figure_size(1200,780); // 设置图片画布的大小 1200*780
matplotlibcpp::suptitle("Total Data Plot"); // 设置分布的总标题名称
matplotlibcpp::subplot2grid(2, 2, 0, 0); // 设置子图在2*2矩阵大小中的(0, 0)位置,即左上角,占据行列大小为默认(1, 1),完整的函数为:subplot2grid(2, 2, 0, 0, 1, 1);
matplotlibcpp::plot(time, roll, {{"color", "red"}, {"label", "roll"}}); // 子图的线数据,颜色,标签
matplotlibcpp::plot(time, pitch, {{"color", "blue"}, {"label", "pitch"}});
matplotlibcpp::plot(time, yaw, {{"color", "forestgreen"}, {"label", "yaw"}});
matplotlibcpp::title("Angel Plot"); // 子图的标题
matplotlibcpp::legend({{"loc", "upper left"}}); // 显示子图的标签,位置:“best”, “upper left”, “upper center”, “upper left”, “center left”, “center”, “center right” (= “right”), “lower left”, “lower center”, “lower right”
//matplotlibcpp::save("../log/Angel.png");
matplotlibcpp::subplot2grid(2, 2, 0, 1);
matplotlibcpp::plot(time, vx, {{"color", "red"}, {"label", "vx"}});
matplotlibcpp::plot(time, vy, {{"color", "blue"}, {"label", "vy"}});
matplotlibcpp::plot(time, vz, {{"color", "forestgreen"}, {"label", "vz"}});
matplotlibcpp::title("Vel Plot");
matplotlibcpp::legend({{"loc", "upper right"}});
//matplotlibcpp::save("../log/Vel.png");
matplotlibcpp::save("../log/Total.png"); // 保存整个图到路径文件中
// 绘制三维坐标系
matplotlibcpp::plot3(x, y, z, {{"color", "blue"}, {"label", "path"}});
matplotlibcpp::xlabel("x"); // 设置x轴标签
matplotlibcpp::ylabel("y"); // 设置y轴标签
matplotlibcpp::set_zlabel("z-floor"); // 设置z轴标签
matplotlibcpp::title("Pose Plot");
matplotlibcpp::legend();
matplotlibcpp::save("../log/Pose.png");