python中的matplotlib画图工具十分便利,想在c++中使用
1.介绍
matplotlibcpp,是简单的C++绘图库。其底层是利用的python中的工具包matplotlib绘图的,所以在使用matplotlibcpp前需要安装python。使用方法类似于python中的工具包matplotlib。
下载地址:https://github.com/lava/matplotlib-cpp
2.python环境配置
python推荐使用anaconda进行安装;相关教程很多,可以自行百度。
(1) 创建新的python环境,打开Anaconda,点击Create。
(2) 输入名称,和选择python版本,查看默认安装位置,可查看https://blog.51cto.com/u_13727606/5307782设置默认安装目录。
(3) 下载matplotlib,因为matplotlibcpp.h中存储的基本是函数接口,具体执行函数还是在matplotlib包中。
(4) 勾选,点击Apply后,可查看依赖包。
(5) matplotlib完成安装。
(6) 同样安装QT,显示的时候用到。
(7) 环境变量添加。
3.VS2019配置matplotlibcpp
(1)把下载的matplotlib-cpp里面的matplotlibcpp.h载入到头文件中。
并修改353-356
static_assert(sizeof(long long) == 8, "long type must occupy 8 bytes");
//template <> struct select_npy_type<long long> { const static NPY_TYPES type = NPY_INT64; };
static_assert(sizeof(unsigned long long) == 8, "long type must occupy 8 bytes");
//template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
(2)配置附加包含目录,附加库目录,附加依赖项
(3)把python3.dll,pyhton39.dll, vcruntime140.dll拷贝到release目录下
然后就是报错解决
4.报错解决方法:
(1)Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
因为用了Anaconda的python!!!
在cmd中输入python也会提示同样的问题!
把PYTHONHOME 和 PYTHONPATH改成conda的目录即可!
(2)This application failed to start because it could not find or load the Qt platform plugin
没有指定QT的dll库。方法一:把anaconda\pkgs\qt-5.9.7-vc14h73c81de_0\Library\plugins\platforms文件夹拷贝到vs2019项目release目录下;
方法二:设置环境变量,如下:
没有报错后,运动可以正常出图。
参考:https://blog.csdn.net/weixin_44272195/article/details/1268373