gpt4 book ai didi

matplotlib 的 C++ 接口(interface)

转载 作者:IT老高 更新时间:2023-10-28 23:01:02 28 4
gpt4 key购买 nike

我想知道是否有可以从 C++ 使用的 matplotlib 接口(interface)。 (也许类似于 gnuplot 的东西)

最佳答案

基于 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;
}

(请注意,我没有检查这个错误,所以可能有一些错误,但你明白了,是的,这是一个非常丑陋的解决方案)。

关于matplotlib 的 C++ 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024737/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com