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
