linux下vscode环境c++调用python的matplotlib库进行绘图

       在vim中可以直接使用以下命令:        

                        g++ 文件名.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7        

进行编译生成可执行文件a.out并能成功运行。

        但是在vscode中编译时 由于matplotlib不是系统自带的库文件,导致vscode正常编译找不到相应的文件,会出现包含头文件时不识别,以及未定义的引用等错误。

解决方案:首先确保系统安装了python2.7(或其他版本),numpy,并且下载好了matplotlibcpp.h头文件(https://github.com/lava/matplotlib-cpp)。

        编译之前需要先进行配置,添加自己库文件所在的路径,链接对应库文件:

1.配置tasks.json:  将"-I","/usr/include/python2.7","-lpython2.7"添加到args中,如下所示:

2.配置c_cpp_properties.json:将自己安装的python2.7,numpy和下载的matplotlibcpp.h头文件所在的路径添加到includePath中,如下所示:

然后就可以正常编译并运行了,结果如下:

测试所用代码:

#include "matplotlibcpp.h"

#include <cmath>

#include <vector>

namespace plt = matplotlibcpp;

int main()

{

// Prepare data.

std::vector<int> x = {1, 2, 3, 4, 4, 5, 6};

std::vector<int> y = {1, 3, 2, 4, 4, 5, 6};

// Set the size of output image to 1200x780 pixels

plt::figure_size(1200, 780);

// Plot line from given x and y data. Color is selected automatically.

plt::plot(x, y);

// Set x-axis to interval [0,1000000]

//plt::xlim(0, 1000*1000);

// Add graph title

plt::title("Sample figure");

// Enable legend.

//plt::legend();

// Save the image (file format is determined by the extension)

plt::save("./basic.png");

plt::show();

}

参考文章:https://blog.csdn.net/weixin_43769166/article/details/118365416


 

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值