首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >matplotlib的C++接口

matplotlib的C++接口
EN

Stack Overflow用户
提问于 2011-11-06 10:02:46
回答 2查看 8.7K关注 0票数 13

我想知道是否有一个可以在C++中使用的matplotlib接口。(可能与gnuplot类似)

EN

回答 2

Stack Overflow用户

发布于 2011-11-07 08:11:06

基于this SO question,您可以使用字符串:

对于静态数据,这非常简单:

代码语言:javascript
复制
#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;
}

这会有点棘手,但对于变量数据仍然可以,只需将其连接到一个字符串即可。

代码语言:javascript
复制
#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,但是您明白了,是的,这是一个非常丑陋的解决方案)。

票数 10
EN

Stack Overflow用户

发布于 2020-05-06 16:03:23

这是一个老问题,但是有一个C++应用程序接口可以使用Mathplot:matplotlib-cpp是从2014年开发的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8024737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档