qt c++呼叫python的matplotlib指令碼
目的: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
"importnumpyasnp\n"
"importmatplotlib.pyplotasplt\n"
"frommpl_toolkits.mplot3dimportAxes3D\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*
"y=r*np.cos(theta)\n"
"ax.plot(x,y,z,label='parametriccurve')\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程式!