目的:qt调用python 的脚本,实现绘制3d曲线的功能
1. 需安装的软件
(64位和32位机器上均可安装如下的软件)
2. 环境变量
Path变量中添加:
添加变量Pythonpath
值:
D:\Python27;D:\Python27\Lib;D:\Python27\DLLs
3. Pro文件中添加:
INCLUDEPATH += D:\Python27\include
LIBS += D:\libpython27.a
4. .cpp文件中添加:
#include "python.h"
5. 调用代码
Py_Initialize();
PyRun_SimpleString("import matplotlib as mpl\n"
"import numpy as np\n"
"import matplotlib.pyplot as plt\n"
"from mpl_toolkits.mplot3d import Axes3D\n"
"\n"
"mpl.rcParams['legend.fontsize'] = 10\n"
"\n"
"fig = plt.figure()\n"
"ax = fig.gca(projection='3d')\n"
"theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)\n"
"z = np.linspace(-2, 2, 100)\n"
"r = z**2 + 1\n"
"x = r * np.sin(theta)\n"
"y = r * np.cos(theta)\n"
"ax.plot(x, y, z, label='parametric curve')\n"
"ax.legend()\n"
"\n"
"plt.show()\n");
Py_Finalize();
运行如上代码:
注:
调试状态下运行会输出如下错误:
Traceback (most recent call last):
File "<string>", line 4, in <module>
ImportError: No module named mpl_toolkits.mplot3d
可以直接运行编译出来的exe程序!