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

what is an easy / easiest way to *plot* a std::vector<double>?

本文关键字:lt double gt 方法 简单 绘制 std vector 什么 最简单      更新时间:2023-10-16

我正在寻找类似的东西:

 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图或其他什么,我在VTK图表中使用过这些东西。我只想出现一个愚蠢的、贫民窟的阴谋。这是为了进行粗略的调试检查,比如"矢量是否发生了变化?当我移动相机时,它是否突然抖动?"等等。

如果这是为了调试,为什么不将矢量输出到一个分隔文件中,并在excel或gnuplot中绘图,或者作为一个单独的步骤?

所以像这样的东西

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

然后用你喜欢的工具绘制文件,例如

gnuplot
plot "myfile" with lines

这个线程似乎对这个问题有很多建议。我还没有看到任何一个简单的图书馆能达到你想要的目的。

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

koolplot

GOBLIN

您可以使用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

使用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

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