Scatter Plots in C++ [closed]

2019-02-04 04:51发布

问题:

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?

回答1:

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.



回答2:

If you are looking for a C++ library rather than I independent plotting tool like gnuplot, I would consider the following:

  • Koolplot
  • dislin (Wikipedia article on dislin)

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.



回答3:

Very heavy solution: you could link against ROOT, which will do just about anything you want:

  • runs on Mac, Windows and Linux
  • runs compiled or using the cint interperter
  • output to a file in encapsulated postscript, PDF, gif, png...
  • display to the screen using several different technologies
  • serialize the data in an internal format that can be manipulated later

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.



回答4:

Good old GNU, they have everything...

http://directory.fsf.org/project/plotutils/



回答5:

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.



回答6:

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.



回答7:

If you're familiar with matplotlib, you can embed python in C/C++ applications. Depending on what you want it for, this might be a quick solution.



回答8:

Regards plotting in C++ for anyone who didn't do it yet. I will say what I did to plot Graphs in C++

  1. Download the zipped file "gp443win32.zip" from http://sourceforge.jp/projects/sfnet_gnuplot/downloads/gnuplot/4.4.3/gp443win32.zip/

  2. Extract it in "C:\Gnuplot" then I read "C:\Gnuplot\gnuplot\README.Windows" and I got these information:

  3. I used Windows 7 ==> so you need to download "winhlp32.exe"... just search for it "winhlp32.exe Windows 7" in a search engine and go through the first link.

  4. Append to the "Path" Environment variable the path for binary folder which is "C:\Gnuplot\gnuplot\binary"

  5. then I shutdown my computer and open it again. After I found that it didn't sense the change in the environment variables :D

  6. you need now to write a sample code to test your plotting ability :) so you can visit this link http://code.google.com/p/gnuplot-cpp/source/browse/#svn%2Ftrunk to see the header file "gnuplot_i.hpp" and source file "example.cc".

You will get many and many graphs, choose your appropriate graph, customize your core, and enjoy :)



回答9:

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.