环境: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