*绘制* std::vector的简单/最简单的方法是什么?

发布于 2024-11-16 06:55:29 字数 337 浏览 2 评论 0原文

我正在寻找类似的东西:

 std::vector<double> X = some_math_function( );
 somenamespace :: plot(  Wrapper( X ) ); // pop-up and display a graph of X on y-axis, 1 to X.size() on x-axis. 

显然有更重量级的方法,例如设置 gnu-plot 或其他什么,并且我已经在 VTK 图表中使用了这些内容。我只是想让一个愚蠢的、贫民窟的情节出现。这是为了进行粗略的调试检查,例如“矢量是否在变化?当我移动相机时它是否突然抖动?”等等。

I'm looking for something like:

 std::vector<double> X = some_math_function( );
 somenamespace :: plot(  Wrapper( X ) ); // pop-up and display a graph of X on y-axis, 1 to X.size() on x-axis. 

Obviously there are heavier-weight methods like setting up gnu-plot or whatever, and I've used the stuff in VTK charts. I just want a stupid, ghetto, plot to appear. This is for coarse debug checking things like "is the vector even changing? does it suddenly jerk when I move the camera?" and so on.

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

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

发布评论

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

评论(5

月亮是我掰弯的 2024-11-23 06:55:29

如果这是为了调试,为什么不将向量输出到分隔文件并在 excel 或 gnuplot 或其他内容中作为单独的步骤进行绘图?

所以就像

//untested
ofstream myfilestream("myfile");
std::copy(X.begin(), X.end(), std::ostream_iterator<double>(myfilestream, '\n');

然后只需在您喜欢的工具中绘制文件即可,例如

gnuplot
plot "myfile" with lines

If this is for debugging why not just output the vector to a delimited file and plot in excel or gnuplot or something as a separate step?

so something like

//untested
ofstream myfilestream("myfile");
std::copy(X.begin(), X.end(), std::ostream_iterator<double>(myfilestream, '\n');

then just plot the file in what ever tool you like e.g.

gnuplot
plot "myfile" with lines
橪书 2024-11-23 06:55:29

这个帖子似乎对此事有很多建议。我还没有看到任何能够满足您想要的目的的简单库。

这里有一些轻量级的例子,但在我看来,如果你必须学习足够的知识来支持任何库,你也可以支持像 gnuplot 这样受人尊敬的库。在许多情况下,您因处理更复杂的库而损失的时间可以通过社区支持和(相对)无错误......更成熟的产品来弥补。

koolplot

妖精

This thread seems to have quite a few suggestions on the matter. I haven't seen anything that stands out as a simple library for the purposes you want.

Here are a few lightweight examples, but it seems to me that if you've got to learn enough to stand up any library, you may as well stand up a respected one like gnuplot. In many cases the time you lose by having to deal with a more complex library is more than made up for by the community support and (relative) bugless..ness... of a more mature product.

koolplot

GOBLIN

不爱素颜 2024-11-23 06:55:29

您可以使用 MathGL (跨平台 GPL 绘图库)。代码看起来像

mglGraphZB gr;// create canvas
mglData d; d.Set(X);  // convert to internal format
gr.YRange(d); // set range for y-axis
gr.Plot(d);   // plot it
gr.Axis();    // draw axis if you need
gr.WritePNG("1.png"); // save it

You can use MathGL (cross-platform GPL plotting library). The code look as

mglGraphZB gr;// create canvas
mglData d; d.Set(X);  // convert to internal format
gr.YRange(d); // set range for y-axis
gr.Plot(d);   // plot it
gr.Axis();    // draw axis if you need
gr.WritePNG("1.png"); // save it
如果没有 2024-11-23 06:55:29

使用 C++11:我建议使用 matplotlibcpp 它使用 python 进行绘图。该库使用起来非常简单,您只需将头文件复制到存储库中即可。

代码如下所示:

#include "matplotlibcpp.h"
#include <vector>
#include <algorithm> // for std::iota

int main()
{
  std::vector <double> y = {0.1, 0.2, 0.4, 0.8, 1.6};
  std::vector <int> x(y.size());
  std::iota(x.begin(), x.end(), 0);

  matplotlibcpp::plot(x, y);
  matplotlibcpp::show();
  plt::save("plot.png");
}

在您的 cmake 中:

find_package(PythonLibs 2.7)
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(myproject ${PYTHON_LIBRARIES})

或者将其直接传递给您的编译器:

g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7

Using C++11: I would recommend using matplotlibcpp which uses python for plots. The library is REALLY straightforward to use and you only need to copy the header file in your repository.

The code would looks like:

#include "matplotlibcpp.h"
#include <vector>
#include <algorithm> // for std::iota

int main()
{
  std::vector <double> y = {0.1, 0.2, 0.4, 0.8, 1.6};
  std::vector <int> x(y.size());
  std::iota(x.begin(), x.end(), 0);

  matplotlibcpp::plot(x, y);
  matplotlibcpp::show();
  plt::save("plot.png");
}

In your cmake :

find_package(PythonLibs 2.7)
target_include_directories(myproject PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(myproject ${PYTHON_LIBRARIES})

Or pass it to directly to your compiler:

g++ main.cpp -std=c++11 -I/usr/include/python2.7 -lpython2.7
煮茶煮酒煮时光 2024-11-23 06:55:29

我记得在 php 中使用 gd 很容易绘制曲线,但那是很久以前的事了。

I remember being easy to plot curves with gd in php, but that was a long time ago.

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