我想知道是否有一个可以在C++中使用的matplotlib接口。(可能与gnuplot类似)
发布于 2011-11-07 08:11:06
基于this SO question,您可以使用字符串:
对于静态数据,这非常简单:
#include "Python.h"
int main()
{
Py_Initialize();
PyRun_SimpleString("import pylab");
PyRun_SimpleString("pylab.plot(range(5))");
PyRun_SimpleString("pylab.show()");
Py_Exit(0);
return 0;
}这会有点棘手,但对于变量数据仍然可以,只需将其连接到一个字符串即可。
#include <string>
#include "Python.h"
using namespace std;
int main()
{
Py_Initialize();
int x[5] = {0, 1, 2, 3, 4};
int y[5] = {5, 1, 7, 5, 1};
string command = "pylab.plot([";
for(int i = 0; i < 4; i++) {
command += x[i];
command += ", ";
}
command += x[4];
command += "], [";
for(int i = 0; i < 4; i++) {
command += y[i];
command += ", ";
}
command += y[4];
command += "])";
PyRun_SimpleString("import pylab");
PyRun_SimpleString(command.c_str());
PyRun_SimpleString("pylab.show()");
Py_Exit(0);
return 0;
}(请注意,我没有检查这里的bug,所以可能有一些bug,但是您明白了,是的,这是一个非常丑陋的解决方案)。
发布于 2020-05-06 16:03:23
这是一个老问题,但是有一个C++应用程序接口可以使用Mathplot:matplotlib-cpp是从2014年开发的
https://stackoverflow.com/questions/8024737
复制相似问题