c++ 绘图包[已关闭]

问题描述 投票:0回答:6

我有一个关于 c++ 绘图包的问题。过去几年我一直在使用 python 和 matplotlib,现在我使用 c++,我想找到类似于 matplotlib (http://matplotlib.sourceforge.net/gallery.html) 的东西,比如 2d、3d 绘图,直方图等等。我只是想知道你的推荐。

c++ simulation plot
6个回答
25
投票

这个答案对于像我这样从 MATLAB(或其他一些成熟的科学编程工具)转向 C++(一种原始编程语言)的人来说可能很有用。


在 C++ 中绘图是一项有点棘手的工作,因为任何 C++ IDE 中都没有可用的默认绘图库。然而,有许多在线库可以使 C++ 中的绘图成为可能。上面的答案中已经提到了一些绘图工具,例如 Gnuplot、PPlot 等,但是我已经一一列出了相关示例,

  1. Koolplot 一个简单、优雅且易于使用的 2D 绘图工具,但可能不足以满足您的要求。下面显示了从网站摘录的示例,您可以在这里找到更多示例以及将其与C++ IDE链接的过程。
```
#include "koolplot.h"
int main()
{
   plotdata x(-6.0, 6.0);    
   plotdata y = sin(x) + x/5;
   plot(x, y);
   return 0;
}
```
  1. GNUPlot,是一个非常强大的开源绘图工具,借助名为 Gnuplot-iostream 接口的接口,从 C++ 调用 gnuplot 命令非常简单。如果有人已经有 gnuplot 绘图经验并且必须使用 C++ 进行编程,那么这个接口非常有用。或者,如果您想创建自己的界面,here 中提供的信息将非常有用。链接这个接口的过程非常简单,只需在你的系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C++ IDE,然后就可以开始了。 here给出了如何使用 gnuplot-iostream 接口在 C++ 中使用 Gnuplot 的示例,下面发布了示例示例的摘录。

    #include <vector>
    #include <cmath>
    #include <boost/tuple/tuple.hpp>
    #include "gnuplot-iostream.h"
    int main() {
        Gnuplot gp;
        std::vector<boost::tuple<double, double, double, double> > pts_A;
        std::vector<double> pts_B_x;
        std::vector<double> pts_B_y;
        std::vector<double> pts_B_dx;
        std::vector<double> pts_B_dy;
        for(double alpha=0; alpha<1; alpha+=1.0/24.0) {
            double theta = alpha*2.0*3.14159;
            pts_A.push_back(boost::make_tuple(
                 cos(theta),
                 sin(theta),
                -cos(theta)*0.1,
                -sin(theta)*0.1
            ));
    
            pts_B_x .push_back( cos(theta)*0.8);
            pts_B_y .push_back( sin(theta)*0.8);
            pts_B_dx.push_back( sin(theta)*0.1);
            pts_B_dy.push_back(-cos(theta)*0.1);
        }
        gp << "set xrange [-2:2]\nset yrange [-2:2]\n";
        gp << "plot '-' with vectors title 'pts_A', '-' with vectors title 'pts_B'\n";
        gp.send1d(pts_A);
        gp.send1d(boost::make_tuple(pts_B_x, pts_B_y, pts_B_dx, pts_B_dy));
    } // very simple tool right???
    
  2. MATLAB(是的,我不是在开玩笑,MATLAB 可以从 C++ 调用)如果您熟悉 MATLAB,您可以通过从 MATLAB 调用函数/工具箱在 C++ 中获得相同的功能,反之亦然。由于 MATLAB 是商业软件,因此首先您必须获得许可证(这是非常昂贵的)。如果您安装了 MATLAB 软件,然后使用

    engine.h
    文件并将必要的 MATLAB 库文件链接到 C++ IDE,则过程非常简单。 herehere 提供了将 Matlab 连接到 Visual Studio c++ 的详细分步过程。这里给出了示例代码,下面给出了示例的摘录:

    来源

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include "engine.h"
    #define  BUFSIZE 256
    
    int main()
    
    {
        Engine *ep;
        mxArray *T = NULL, *result = NULL;
        char buffer[BUFSIZE+1];
        double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };
        if (!(ep = engOpen(""))) {
            fprintf(stderr, "\nCan't start MATLAB engine\n");
            return EXIT_FAILURE;
        }
        T = mxCreateDoubleMatrix(1, 10, mxREAL);
        memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
        engPutVariable(ep, "T", T);
        engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
        engEvalString(ep, "plot(T,D);");
        engEvalString(ep, "title('Position vs. Time for a falling object');");
        engEvalString(ep, "xlabel('Time (seconds)');");
        engEvalString(ep, "ylabel('Position (meters)');");
    
        printf("Hit return to continue\n\n");
        fgetc(stdin);
    
        printf("Done for Part I.\n");
        mxDestroyArray(T);
        engEvalString(ep, "close;");
    
        buffer[BUFSIZE] = '\0';
        engOutputBuffer(ep, buffer, BUFSIZE);
        while (result == NULL) {
            char str[BUFSIZE+1];
            printf("Enter a MATLAB command to evaluate.  This command should\n");
            printf("create a variable X.  This program will then determine\n");
            printf("what kind of variable you created.\n");
            printf("For example: X = 1:5\n");
            printf(">> ");
    
            fgets(str, BUFSIZE, stdin);
            engEvalString(ep, str);
            printf("%s", buffer);
            printf("\nRetrieving X...\n");
            if ((result = engGetVariable(ep,"X")) == NULL)
              printf("Oops! You didn't create a variable X.\n\n");
            else {
            printf("X is class %s\t\n", mxGetClassName(result));
            }
        }
        printf("Done!\n");
        mxDestroyArray(result);
        engClose(ep);
    
        return EXIT_SUCCESS;
    }
    
  3. Python,人们喜欢提问者(熟悉Python中的matplotlib工具)。有一个非常优雅的接口可以从 C++ 调用 python。一个简单的例子可能看起来像这样(source)并且

    matlabplotcpp.h
    可以在here获得。

    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    int main() {
        plt::plot({1,3,2,4});
        plt::show();
    }
    

12
投票

我再次推荐 gnuplot。

如果你不想使用它,那么我在使用 plplot 时就喜欢它:http://plplot.sourceforge.net/。如果您想向绘图添加按钮,也可以将 plplot 的画布放入 gtk+ 框架中。

也就是说,我很快就回到了 gnuplot。


5
投票

您尝试过使用Gnuplot吗?还提供 C++ 接口


4
投票

有 GPL 库 MathGL,它是用 C++ 编写的,具有 Python/C/Fortran 等接口。它也可以制作很多 3D 绘图。


2
投票

我曾经使用过Qwt。它是 Qt 之上的一个库,提供了许多非常有用的绘图工具。如果这实际上是一个商业项目,请注意 Qt 许可费用。


1
投票

我在学校的时候问过我的教授类似的问题,他说 C++ 并不是真正用于制作数据图之类的。他没有提出任何建议,但话又说回来,他更多的是一名嵌入式系统开发人员,并且没有将 C++ 用于科学计算应用程序。他的建议是用 C++ 进行所有数据处理,将结果保存在某个地方,然后使用 Python 或 R 导入结果,然后在那里进行数据分析。另一种选择是您可以使用 R 包 Rcpp 包。它允许您在 R 脚本或 Markdown 中编写 C++ 函数。 rcpp 包

© www.soinside.com 2019 - 2024. All rights reserved.