C++ 调用matplotlibcpp 绘图配置说明

环境:ubuntu20 , vscode

1、配置python环境,安装python3;

sudo apt-get install python3-dev

2、配置 numpy 和 matplotlib;

sudo apt-get update
sudo apt-get install python3-numpy
sudo apt-get install python3-matplotlib

 检查是否安装成功:

终端进入python环境下:import matplotlib.pyplot as plt

3、下载matplotlibcpp包,这个包可以放在任何位置,后续把路径添加就OK;

git clone https://github.com/lava/matplotlib-cpp

4、使用VScode

        (1)、c_cpp_properties.json文件,修改如下:

 找到”matplotlib-cpp“的路径:

         (2)、cmake.list修改


 find_package(Python3 COMPONENTS Development NumPy)
 
 target_include_directories(test_every PRIVATE ${Python2_INCLUDE_DIRS} ${Python2_NumPy_INCLUDE_DIRS})
 
 include_directories("/home/bobo/third_storage/matplotlib-cpp/")
 
 target_link_libraries(test_every Python3::Python Python3::NumPy)

其中“test_every" 为自己的可执行文件,一定放在该可执行文件add_executable的后面

5、测试,写可执行文件

#include "matplotlibcpp.h"
#include <cmath>

namespace plt = matplotlibcpp;

int main()
{
    // Prepare data.
    int n = 5000;
    std::vector<double> x(n), y(n), z(n), w(n,2);
    for(int i=0; i<n; ++i) {
        x.at(i) = i*i;
        y.at(i) = sin(2*M_PI*i/360.0);
        z.at(i) = log(i);
    }

    // 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);
    // Plot a red dashed line from given x and y data.
    plt::plot(x, w,"r--");
    // Plot a line whose name will show up as "log(x)" in the legend.
    plt::named_plot("log(x)", x, z);
    // 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();
}
————————————————
版权声明:本文为CSDN博主「Kevin_Xie86」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Kevin_Xie86/article/details/125692259

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值