C++中调用matplotlibcpp.h画图测试笔记

一、相关的程序码仓地址

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");
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值