什么是*绘制*std::vector的简单/最简单的方法<double>?
what is an easy / easiest way to *plot* a std::vector<double>?
我正在寻找类似的东西:
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绘制曲线很容易,但那是很久以前的事了。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ