What is the best way to graph scatter plots in C++?
Do you write data to a file and use another tool? Is there a library like matplotlib in Python?
What is the best way to graph scatter plots in C++?
Do you write data to a file and use another tool? Is there a library like matplotlib in Python?
This is certainly not the best way but I usually write output files that can be read by R and use this, along with an appropriate script, to plot the graphs.
I always write out data and then using gnuplot to create my graphs. It is by far the best way I have found of producing graphs in a variety of formats: eps, png, jpeg, xpm, you name it.
gnuplot
will do scatter plot very easily. Provided the x
and y
values are in 2 space-separated columnss, then
plot "data.txt" using 1:2
Will give you a quick scatter plot. Then you can adjust it and what not using other gnuplot commands.
If you are involved in sciences, then learning gnuplot
will be very valuable to you. It kicks the crap out of doing excel plots for sure and it eases the task of making plots to include in papers.
If you are looking for a C++ library rather than I independent plotting tool like gnuplot, I would consider the following:
dislin seems to be the more interesting of the two. Here is a description extracted from the wikipedia article:
DISLIN is a high-level and easy to use plotting library developed by Helmut Michels at the Max Planck Institute in Katlenburg-Lindau, Germany. Helmut Michels currently works as a mathematician and Unix system manager at the computer center of the institute.
The DISLIN library contains routines and functions for displaying data as curves, bar graphs, pie charts, 3D-colour plots, surfaces, contours and maps. Several output formats are supported such as X11, VGA, PostScript, PDF, CGM, HPGL, SVG, PNG, BMP, PPM, GIF and TIFF.
DISLIN is available for the programming languages Fortran 77, Fortran 90/95 and C. Plotting extensions for the languages Perl, Python and Java are also supported for most operating systems. The current version of DISLIN is 9.4, released in October 2008. The first version 1.0 was released in December 1986.
The DISLIN software is free for non-commercial use.
The problem here is that C++, unlike Java for example, does not have built-in GUI or graphics. If you want to generate graphs with C++ you would need to use a GUI library available for your OS. There are free GUI libraries, many cross-plaform such as Qt or GTK.
However, as other people have pointed out, the easiest thing for you to do would be to save the data into a text file, and use another program to generate the graph. gnuplot is definitely a good choice. It comes standard with most linux distros, and you get for Windows under cygwin.
Chart Director has bindings for C++. I've used their .Net libraries, and I've been pretty happy with them. It's a pretty cheap library, and gives you the power to do all sorts of different charts.
Very heavy solution: you could link against ROOT, which will do just about anything you want:
Sure, its a bit much for most people, but it does do exactly what you asked for. I use it because I know it and it is already on my machines becase I'm that kind of physicist.