C++ 绘图包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4283731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:01:53  来源:igfitidea点击:

plotting package for c++

c++simulationplot

提问by nykon

I have got a question about plotting package for c++. For last few years I was using python and matplotlib, right now I am using c++ and I would like to find something similar to matplotlib (http://matplotlib.sourceforge.net/gallery.html) like 2d, 3d plots, histograms and so on and so on. I just want to know Your recommendation.

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

Best regards, nykon

最好的问候,尼康

回答by Tom

I again would recommend gnuplot.

我再次推荐 gnuplot。

If you don't want to use it then I liked plplot when I used it: http://plplot.sourceforge.net/. The canvas to plplot can be put into gtk+ frame too if you want to add buttons to your plot.

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

That said, I returned to gnuplot before too long.

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

回答by darioo

Have you tried using Gnuplot? A C++ interfaceis also available.

你试过使用Gnuplot吗?甲C ++接口也可用。

回答by The_Learner

The questioner might have already got the answer. However, this answer may be useful for people like me who shifted from MATLAB (or some other well-developed scientific programming tools) to C++ (A primitive programming language).

提问者可能已经得到了答案。但是,这个答案可能对像我这样从 MATLAB(或其他一些开发良好的科学编程工具)转向 C++(一种原始编程语言)的人有用。

Plotting is a little bit tricky job in C++, as there is no default plotting library available in any C++ IDE. However, there are many libraries available online for making plotting possible in C++. Some plotting tools like Gnuplot, PPlot, etc are already mentioned in the above answers, however, I have listed one by one with relevant examples,

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

  1. Koolplota simple, elegant and easy to use tool for 2D plotting and may not be enough for your requirement. An example excerpt taken from the websiteis shown below, you can find more examples and the process of linking it with 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, is a very robust opensource tool for plotting, with the help of an interface called Gnuplot-iostream interface, calling the gnuplot commands from C++ is very easy process. If somebody is already experienced plotting in gnuplot and have to use C++ for their programming then this interface is very usefull. or if you want to create your own interface, this the information provided in herewill be very useful. The process of linking this interface is veryeasy, just install gnuplot in your system and then link the include directory and lib directory of gnuplot to C++ IDE, and then you are ready to go. Examples on how to use Gnuplot from C++ using gnuplot-iostream interface are given here, an excerpt of sample example is posted below.

    #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(Yes, I am not kidding MATLAB can be called from C++) If you are familiar with MATLAB, you can get the same functionality in C++ by calling, functions/toolboxes from MATLAB and vice versa. Since MATLAB is commercial software, first you have to acquire license (this is very costly). If you have an installed MATLAB software, then use the engine.hfile and link the necessary MATLAB library files to C++ IDE, then the process is outright simple. A detailed step-by-step process of linking matlab to visual studio c++ is provided hereand here. An example code is given here, an excerpt of an example is given below enter image description here(source)

    #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] = '
    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    int main() {
        plt::plot({1,3,2,4});
        plt::show();
    }
    
    '; 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, people like questioner (who are familiar with matplotlib tool in Python). A very elegant interface is available to call python from C++. a simple example may look like this (source) and the matlabplotcpp.his available here.

    #include "koolplot.h"
    int main()
    {
       plotdata x(-6.0, 6.0);    
       plotdata y = sin(x) + x/5;
       plot(x, y);
       return 0;
    }
    

    Hope this information may be useful...

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

    #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. GNUPlot是一个非常强大的绘图开源工具,借助名为Gnuplot-iostream interface 的接口,从 C++ 调用 gnuplot 命令是非常简单的过程。如果有人已经在 gnuplot 中绘图并且必须使用 C++ 进行编程,那么这个界面非常有用。或者,如果您想创建自己的界面,此处提供的信息将非常有用。链接这个界面的过程很简单,只要在你的系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C++ IDE,就可以了。此处给出如何使用 gnuplot-iostream 接口从 C++ 使用 Gnuplot的示例,下面发布了示例示例的摘录。

    #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] = '
    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    int main() {
        plt::plot({1,3,2,4});
        plt::show();
    }
    
    '; 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. MATLAB(是的,我不是在开玩笑,MATLAB 可以从 C++ 调用)如果您熟悉 MATLAB,您可以通过从 MATLAB 调用函数/工具箱来获得 C++ 中的相同功能,反之亦然。由于 MATLAB 是商业软件,首先您必须获得许可(这是非常昂贵的)。如果您安装了 MATLAB 软件,然后使用该engine.h文件并将必要的 MATLAB 库文件链接到 C++ IDE,那么该过程非常简单。此处此处提供了将 matlab 链接到 Visual Studio C++ 的详细分步过程。这里给出了一个示例代码,下面给出了一个示例的摘录在此处输入图片说明来源

    ##代码##
  4. Python,人们喜欢提问者(熟悉 Python 中的 matplotlib 工具)。一个非常优雅的接口可用于从 C++ 调用 python。一个简单的示例可能看起来像这样(),并且matlabplotcpp.h可以在此处获得

    ##代码##

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

Note- If any information is not cited appropriately please comment I will cite the source inforamtion…

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

回答by abalakin

There is GPL library MathGLwhich is written in C++ and have interfaces to Python/C/Fortran and so on. It can make a lot of 3D plots too.

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

回答by benso8

I asked my professor similar questions when I was in school and he said C++ isn't really used for making plots of data and such. He didn't have any suggestions but then again he was more of a software developer rather than using C++ for more scientific computing applications. His suggestion was to do all the data crunching in C++ and then find a way to export your code back over to Python or R. Another alternative is you could use the R package Rcpp package. It allows you to write C++ functions inside of an R script or a markdown. rcpp package

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

回答by John Sloper

I used Qwtsome time back. It's a library on top of Qt which provides a number of very useful plotting tools. Beware of Qt licensing fees if this is actually a commercial project.

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