matplotlib-cpp 库使用

引言

欢迎使用 matplotlib-cpp,它可能是最简单的 C++ 绘图库。 它的构建类似于 Matlab 和 matplotlib 使用的绘图 API。
如上其 Readme 介绍:
https://github.com/lava/matplotlib-cpp
第一步git clone source code

1.直接编译使用

# 源码方式默认编译安装 
git clone https://github.com/lava/matplotlib-cpp.git
cd matplotlib-cpp/
mkdir build && cd build 
cmake ..
make
sudo make install

# 安装依赖
sudo apt-get install python-matplotlib python-numpy python2.7-dev

预配置环境已经完成,下面演示一个例子来自官方介绍:

// 创建文件
vim main.cpp

//复制下面代码
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
    plt::plot({1,3,2,4});
    plt::show();
}

// g++ 编译
g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7

// 执行生成程序
./a.out

在这里插入图片描述

2. 使用Cmake,编写CMakeLists.txt

详细看这条指令g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
他提示了我们如何编写CMakeLists文件。
-l 查找头文件 /usr/include/python2.7
-l 查找库文件 python2.7
因此CMakeList.txt文件内容这样编写:

# matplotlib-cpp
find_package(PythonLibs REQUIRED)
    set(PYTHON_INCLUDE_DIRS /usr/include/python2.7)
    set(PYTHON_LIBRARIES /usr/lib/x86_64-linux-gnu/libpython2.7.so)
include_directories(${PYTHON_INCLUDE_DIRS})

....
target_link_libraries(xxx
${PYTHON_LIBRARIES})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值