c++ 绘图包

发布于 2024-10-04 03:36:25 字数 1809 浏览 5 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

來不及說愛妳 2024-10-11 03:36:26

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

I used Qwt some 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.

情丝乱 2024-10-11 03:36:26

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

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 an embedded systems developer and was not using C++ for scientific computing applications. His suggestion was to do all the data crunching in C++, save the results somewhere, and use Python or R to import the results, and then do the data analysis there. 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

幼儿园老大 2024-10-11 03:36: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++ 进行编程,那么这个接口非常有用。或者,如果您想创建自己的界面,此处提供的信息将非常有用。链接这个接口的过程非常简单,只需在你的系统中安装gnuplot,然后将gnuplot的include目录和lib目录链接到C++ IDE,然后就可以开始了。 这里给出了如何使用 gnuplot-iostream 接口在 C++ 中使用 Gnuplot 的示例,摘录下面发布了示例示例。

    #include <向量>
    #include 
    #include ;
    #include“gnuplot-iostream.h”
    int main() {
        Gnuplot gp;
        std::vector > pts_A;
        std::vector pts_B_x;
        std::vector pts_B_y;
        std::vector pts_B_dx;
        std::vector pts_B_dy;
        for(双 alpha=0; alpha<1; alpha+=1.0/24.0) {
            双θ=α*2.0*3.14159;
            pts_A.push_back(boost::make_tuple(
                 余弦(θ),
                 罪(θ),
                -cos(θ)*0.1,
                -sin(θ)*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 << "设置 x 范围 [-2:2]\n设置 y 范围 [-2:2]\n";
        GP << "绘制'-',向量标题为'pts_A','-',向量标题为'pts_B'\n";
        gp.send1d(pts_A);
        gp.send1d(boost::make_tuple(pts_B_x, pts_B_y, pts_B_dx, pts_B_dy));
    } // 非常简单的工具,对吧???
    

  2. MATLAB (是的,我不是开玩笑 MATLAB 可以从 C++ 调用)如果您熟悉 MATLAB,您可以通过从 MATLAB 调用函数/工具箱在 C++ 中获得相同的功能,反之亦然。由于 MATLAB 是商业软件,因此首先您必须获得许可证(这是非常昂贵的)。如果您安装了 MATLAB 软件,然后使用 engine.h 文件并将必要的 MATLAB 库文件链接到 C++ IDE,那么该过程非常简单。提供了将 Matlab 链接到 Visual Studio c++ 的详细分步过程 此处此处。这里给出了示例代码,示例摘录如下:
    输入图片此处描述

    (来源)

    #include ;
    #include ;
    #include ;
    #include“engine.h”
    #定义BUFSIZE 256
    
    int main()
    
    {
        发动机*ep;
        mxArray *T = NULL,*结果 = NULL;
        字符缓冲区[BUFSIZE+1];
        双倍时间[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, "\n无法启动 MATLAB 引擎\n");
            返回 EXIT_FAILURE;
        }
        T = mxCreateDoubleMatrix(1, 10, mxREAL);
        memcpy((void *)mxGetPr(T), (void *)时间, sizeof(时间));
        engPutVariable(ep, "T", T);
        engEvalString(ep, "D = .5.*(-9.8).*T.^2;");
        engEvalString(ep, "绘图(T,D);");
        engEvalString(ep, "title('下落物体的位置与时间');");
        engEvalString(ep, "xlabel('时间(秒)');");
        engEvalString(ep, "ylabel('位置(米)');");
    
        printf("按回车键继续\n\n");
        fgetc(标准输入);
    
        printf("第一部分完成。\n");
        mxDestroyArray(T);
        engEvalString(ep, "关闭;");
    
        缓冲区[BUFSIZE] = '\0';
        engOutputBuffer(ep, 缓冲区, BUFSIZE);
        而(结果== NULL){
            char str[BUFSIZE+1];
            printf("输入要计算的 MATLAB 命令。该命令应该\n");
            printf("创建一个变量X。然后该程序将确定\n");
            printf("你创建了什么类型的变量。\n");
            printf("例如:X = 1:5\n");
            printf(“>>”);
    
            fgets(str, BUFSIZE, 标准输入);
            engEvalString(ep, str);
            printf("%s", 缓冲区);
            printf("\n正在检索 X...\n");
            if ((结果 = engGetVariable(ep,"X")) == NULL)
              printf("哎呀!您没有创建变量 X。\n\n");
            别的 {
            printf("X 是类 %s\t\n", mxGetClassName(result));
            }
        }
        printf("完成!\n");
        mxDestroyArray(结果);
        engClose(ep);
    
        返回退出_成功;
    }
    
  3. Python,人们喜欢提问者(熟悉的人)使用 Python 中的 matplotlib 工具)。有一个非常优雅的接口可以从 C++ 调用 python。一个简单的示例可能如下所示(matlabplotcpp.h 可在此处获取。

    <前><代码>#include“matplotlibcpp.h”
    命名空间 plt = matplotlibcpp;
    int main() {
    plt::绘图({1,3,2,4});
    plt::显示();
    }

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).


Plotting is a 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,

  1. Koolplot a simple, elegant and easy to use tool for 2D plotting and may not be enough for your requirement. An example excerpt taken from the website is 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;
}
```
  1. 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 useful. Or if you want to create your own interface, this is the information provided in here will be very useful. The process of linking this interface is very easy, 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???
    
  2. 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 a license (this is very costly). If you have an installed MATLAB software, then use the engine.h file 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 here and 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] = '\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, 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.h is available here.

    #include "matplotlibcpp.h"
    namespace plt = matplotlibcpp;
    int main() {
        plt::plot({1,3,2,4});
        plt::show();
    }
    
病毒体 2024-10-11 03:36:25

我再次推荐 gnuplot。

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

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

I again would recommend 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.

That said, I returned to gnuplot before too long.

零度℉ 2024-10-11 03:36:25

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

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

篱下浅笙歌 2024-10-11 03:36:25

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文