绘制c ++包

nyk*_*kon 6 c++ simulation plot

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

最好的问候,nykon

The*_*ner 8

提问者可能已经得到了答案。但是,这个答案可能对像我这样从 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. GNUPlot是一个非常强大的绘图开源工具,借助名为Gnuplot-iostream interface 的接口,从 C++ 调用 gnuplot 命令是非常简单的过程。如果有人已经在 gnuplot 中绘图并且必须使用 C++ 进行编程,那么这个界面非常有用。或者如果您想创建自己的界面,这里提供的信息将非常有用。链接这个界面的过程很简单,只要在你的系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C++ IDE,就可以了。此处给出如何使用 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???
    
    Run Code Online (Sandbox Code Playgroud)
  3. MATLAB(是的,我不是在开玩笑 MATLAB 可以从 C++ 调用)如果您熟悉 MATLAB,您可以通过从 MATLAB 调用函数/工具箱来获得 C++ 中的相同功能,反之亦然。由于 MATLAB 是商业软件,首先您必须获得许可(这是非常昂贵的)。如果您安装了 MATLAB 软件,然后使用该engine.h文件并将必要的 MATLAB 库文件链接到 C++ IDE,那么该过程非常简单。此处此处提供了将 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. Python,人们喜欢提问者(熟悉 Python 中的 matplotlib 工具)。一个非常优雅的接口可用于从 C++ 调用 python。一个简单的例子可能看起来像这样(),并且matlabplotcpp.h可以在这里找到

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

    希望这些信息可能有用...

注意- 如果没有正确引用任何信息,请发表评论,我将引用来源信息……


Tom*_*Tom 6

我再次推荐gnuplot.

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

也就是说,我不久就回到了gnuplot.


dar*_*ioo 5

您是否尝试过使用Gnuplot?甲C ++接口也可用。

  • 我也对Gnuplot无法满足您的需求感到惊讶。需要一些调整,但是您可以使用它获得出版物质量图。有关某些示例,请参见http://www.phyast.pitt.edu/~zov1/gnuplot/html/gallery.html。 (2认同)