绘制c ++包

时间:2010-11-26 08:47:36

标签: c++ simulation plot

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

祝你好运, nykon

6 个答案:

答案 0 :(得分:6)

我再次推荐gnuplot。

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

那就是说,我不久就回到了gnuplot。

答案 1 :(得分:5)

您是否尝试过使用Gnuplot?还可以使用C++ interface

答案 2 :(得分:3)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

我在学校时问过教授类似的问题,他说C ++并没有真正用于制作数据图等。他没有任何建议,但他还是更多的是软件开发人员,而不是将C ++用于更科学的计算应用程序。他的建议是使用C ++进行所有数据处理,然后找到一种将代码导出回Python或R的方法。另一种选择是可以使用R包Rcpp包。它允许您在R脚本或markdown内编写C ++函数。 rcpp package

答案 5 :(得分:1)

发问者可能已经找到答案。但是,对于像我这样从MATLAB(或其他一些发达的科学编程工具)转向C ++(原始编程语言)的人来说,此答案可能有用。

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

  1. Koolplot一个简单,优雅且易于使用的2D绘图工具,可能不足以满足您的要求。 website的示例摘录如下所示,您可以找到更多示例以及将其与C ++ IDE here链接的过程。

    #include "koolplot.h"
    int main()
    {
       plotdata x(-6.0, 6.0);    
       plotdata y = sin(x) + x/5;
       plot(x, y);
       return 0;
    }
    
  2. GNUPlot是一个非常强大的开源绘图工具,借助名为Gnuplot-iostream interface的接口,从C ++调用gnuplot命令非常简单。如果有人已经在gnuplot中进行绘图,并且必须使用C ++进行编程,那么此接口非常有用。或者,如果您想创建自己的界面,here中提供的信息将非常有用。链接此接口的过程非常简单,只需在系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C ++ IDE,即可开始使用。 here给出了如何从C ++使用gnuplot-iostream接口使用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???
    
  3. MATLAB(是的,我不是在说MATLAB可以从C ++调用)如果您熟悉MATLAB,则可以通过从MATLAB调用,函数/工具箱以及反过来从C ++获得相同的功能。反之亦然。由于MATLAB是商业软件,因此首先您必须获得许可证(这非常昂贵)。如果您已经安装了MATLAB软件,则使用engine.h文件并将必要的MATLAB库文件链接到C ++ IDE,这样的过程将非常简单。 herehere提供了将matlab链接到Visual Studio c ++的详细分步过程。此处提供了示例代码,enter image description heresource)下方给出了示例摘录

    #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;
    }
    
  4. 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();
    }
    

    希望此信息可能有用...

注意-如果未正确引用任何信息,请评论我将引用源信息…