在 C++ 中绘制图形和图表的免费简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/765408/
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
Free easy way to draw graphs and charts in C++?
提问by syaz
I am doing a little exploring simulation and I want to show the graphs to compare the performance among the algorithms during run-time.
我正在做一些探索模拟,我想显示图表以比较运行时算法之间的性能。
What library comes to your mind? I highly prefer those that come small as I'd love if it's easy for my instructor to compile my code. I've checked gdchartbut it seems to be too heavy. I just want a simple x-y sort of timeline graph.
你会想到什么图书馆?如果我的导师很容易编译我的代码,我非常喜欢那些很小的代码。我检查了gdchart但它似乎太重了。我只想要一个简单的 xy 类型的时间线图。
Google chart is of course out of the question, in case you've read thissimilar question.
谷歌图表当然是不可能的,如果你读过这个类似的问题。
Related post Scatter Plots in C++.
相关文章C++ 中的散点图。
采纳答案by moinudin
My favourite has always been gnuplot. It's very extensive, so it might be a bit too complex for your needs though. It is cross-platform and there is a C++ API.
我最喜欢的一直是gnuplot。它非常广泛,因此对于您的需求来说可能有点过于复杂。它是跨平台的,并且有一个 C++ API。
回答by Bill Lynch
Honestly, I was in the same boat as you. I've got a C++ Library that I wanted to connect to a graphing utility. I ended up using Boost Pythonand matplotlib. It was the best one that I could find.
老实说,我和你在同一条船上。我有一个 C++ 库,我想将它连接到图形实用程序。我最终使用了Boost Python和matplotlib。这是我能找到的最好的。
As a side note: I was also wary of licensing. matplotlib and the boost libraries can be integrated into proprietary applications.
附带说明:我也对许可持谨慎态度。matplotlib 和 boost 库可以集成到专有应用程序中。
Here's an example of the code that I used:
这是我使用的代码示例:
#include <boost/python.hpp>
#include <pygtk/pygtk.h>
#include <gtkmm.h>
using namespace boost::python;
using namespace std;
// This is called in the idle loop.
bool update(object *axes, object *canvas) {
static object random_integers = object(handle<>(PyImport_ImportModule("numpy.random"))).attr("random_integers");
axes->attr("scatter")(random_integers(0,1000,1000), random_integers(0,1000,1000));
axes->attr("set_xlim")(0,1000);
axes->attr("set_ylim")(0,1000);
canvas->attr("draw")();
return true;
}
int main() {
try {
// Python startup code
Py_Initialize();
PyRun_SimpleString("import signal");
PyRun_SimpleString("signal.signal(signal.SIGINT, signal.SIG_DFL)");
// Normal Gtk startup code
Gtk::Main kit(0,0);
// Get the python Figure and FigureCanvas types.
object Figure = object(handle<>(PyImport_ImportModule("matplotlib.figure"))).attr("Figure");
object FigureCanvas = object(handle<>(PyImport_ImportModule("matplotlib.backends.backend_gtkagg"))).attr("FigureCanvasGTKAgg");
// Instantiate a canvas
object figure = Figure();
object canvas = FigureCanvas(figure);
object axes = figure.attr("add_subplot")(111);
axes.attr("hold")(false);
// Create our window.
Gtk::Window window;
window.set_title("Engineering Sample");
window.set_default_size(1000, 600);
// Grab the Gtk::DrawingArea from the canvas.
Gtk::DrawingArea *plot = Glib::wrap(GTK_DRAWING_AREA(pygobject_get(canvas.ptr())));
// Add the plot to the window.
window.add(*plot);
window.show_all();
// On the idle loop, we'll call update(axes, canvas).
Glib::signal_idle().connect(sigc::bind(&update, &axes, &canvas));
// And start the Gtk event loop.
Gtk::Main::run(window);
} catch( error_already_set ) {
PyErr_Print();
}
}