目的
2022/4/8
本地目录: E:\Master\study\Cpp\Plot
想用C++实现Python、Matlab、R语言那样便捷的绘图,网上搜了一圈,发现有给Python的matplotlib做了C++接口的第三方库,于是尝试下载使用。
过程
花了一天时间,完成了配置。
主要参考了三篇博客:
VS项目中配置matplotlib-cpp绘制图片
VisualStudio2019 c++安装matplotlib-cpp
c++调用matplotlib一
配置
- 包含头文件及命名空间
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
- 项目属性→ \rightarrow → 配置:所有配置→ \rightarrow → 平台:所有平台 → \rightarrow → C/C++ → \rightarrow → 常规→ \rightarrow → 附加包含目录 → \rightarrow → 添加python include文件夹和numpy include文件夹的位置
C:\Users\LJB\AppData\Local\Programs\Python\Python38-32\include
C:\Users\LJB\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\numpy\core\include
- 项目属性→ \rightarrow → 链接器→ \rightarrow → 常规→ \rightarrow → 附加库目录→ \rightarrow → 添加python libs文件夹的位置
C:\Users\LJB\AppData\Local\Programs\Python\Python38-32\libs
- 项目属性→ \rightarrow → 链接器→ \rightarrow → 输入→ \rightarrow → 添加依赖项→ \rightarrow → 添加libs文件夹内的.lib文件
python3.lib
python38.lib
_tkinter.lib
- 项目属性→ \rightarrow → 常规→ \rightarrow → C++语言标准→ \rightarrow → ISO C++ 17标准 或 以上
遇到的问题
1
std::stod这句话会报错,原因是系统报std不包含stod函数。
第一篇博客里都提了解决办法,但是这个问题的原因是 matplotlibcpp.h
文件忘记包含 string
头文件了,添加即可
#include
2
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyFunction_Type
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyImport_ImportModule
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyExc_RuntimeError
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyTuple_New
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyDict_SetItemString
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyFloat_FromDouble
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyExc_AttributeError
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyErr_SetString
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyErr_Format
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_Py_Finalize
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_Py_SetProgramName
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp__Py_Dealloc
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_Py_Initialize
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyCapsule_GetPointer
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyList_New
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyObject_GetAttrString
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyDict_New
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyObject_CallMethod
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyTuple_SetItem
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PySys_SetArgv
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyObject_CallObject
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyImport_Import
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyCapsule_Type
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyExc_ImportError
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyList_SetItem
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyErr_Print
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyUnicode_FromString
1>Plot.obj : error LNK2001: 无法解析的外部符号 __imp_PyObject_Call
1>E:\Master\study\Cpp\Plot\x64\Release\Plot.exe : fatal error LNK1120: 28 个无法解析的外部命令
这个是由于测试程序的配置是 x64
,而我安装的是32位的Python,因此不能匹配,所以两种解决思路:
- 安装64位Python
- 配置改为
x86
3
还要注意用Vcpkg安装matplotlib-cpp时,选择对应版本安装
默认安装32位
.\vcpkg install matplotlib-cpp
这样是安装64位
.\vcpkg install matplotlib-cpp:x64-windows
如果下载慢,可以先下载,再编译
默认安装32位
.\vcpkg install matplotlib-cpp --only-downloads
.\vcpkg install matplotlib-cpp
这样是安装64位
.\vcpkg install matplotlib-cpp:x64-windows --only-downloads
.\vcpkg install matplotlib-cpp:x64-windows
最终成功
; 测试代码
本地目录: E:\Master\study\Cpp\Plot
Plot.cpp
:
#include
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main() {
int n = 1000;
std::vector<double> x, y, z;
int count = 1;
for (int i = 0; i < n; i++) {
x.push_back(i * i);
y.push_back(sin(2 * 3.14 * i / 360.0));
z.push_back(log(i));
if (i % 10 == 0) {
plt::clf();
plt::plot(x, y);
plt::named_plot("log(x)", x, z);
plt::xlim(0, n * n);
plt::title("Sample figure");
plt::legend();
std::string pathObj = "animation//" + std::to_string(count);
plt::pause(0.01);
count++;
}
}
}
Original: https://blog.csdn.net/weixin_43012724/article/details/124051588
Author: 赖亦无
Title: 【C++】11 Visual Studio 2019 C++安装matplotlib-cpp绘图
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/764248/
转载文章受原作者版权保护。转载请注明原作者出处!