Creating Graphs in C++: A Beginner's Guide

  • Context: C/C++ 
  • Thread starter Thread starter taylrl3
  • Start date Start date
  • Tags Tags
    Graphs
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 6K views
taylrl3
Messages
60
Reaction score
0
Hi,

I have just started to learn C++ after a year programming with IDL. I realize this is probably a really stupid thing to be asking but what syntax should I use to display my data. To start off with I just want to plot a simple x-y graph. I have my arrays all ready, what do I type? Can C++ even create graphs like IDL can? I'm sure it must.

Cheers
 
Physics news on Phys.org
C++ doesn't have a built in gui library.

You have a few options.
Write a data file and plot it with Excel/Gnuplot/etc
Write the data into an image file and display that
Learn Qt,wxWidgets or some other gui toolkit.
 
Thanks! I had the feeling it couldn't produce graphs directly.
How would I go writing the data out as an image file? Can I include axis etc like this?
 
taylrl3 said:
How would I go writing the data out as an image file? Can I include axis etc like this?
That's possibly the hardest option, you would either do it all yourself (the image is ulimtately just an array of values) or use some plotting library that could also plot to an image.
But it's a good option if you are doing some heavy number crunching type work and just need to output a map of some values.

Easiest is probably gnuplot if you need to produce publication type figures, it has a C++ interface (gunplot++) but haven't used it. You can also call it directly from your program using popen() to run the gnuplot command.

See also http://stackoverflow.com/questions/492893/graph-drawing-c-library
ps. If you are searching it's better to look for 'plot' than 'graph', in CS a graph is generally used in the maths sense, eg a heirachy of data
 
Last edited by a moderator:
taylrl3 said:
Hi,

I have just started to learn C++ after a year programming with IDL. I realize this is probably a really stupid thing to be asking but what syntax should I use to display my data. To start off with I just want to plot a simple x-y graph. I have my arrays all ready, what do I type? Can C++ even create graphs like IDL can? I'm sure it must.

Cheers

Surely your C++ implementation has a graphics library. I say that because I've done a lot of C++ graphics programming. And you can always create your own library with low-level (assembly) constructs by writing directly to the video memory of the output device. It's a large array. Put the right number in the right location of the array, and a pixel corresponding to that location lights up on the screen. Then take it from there although that's quite involved. Usually, though just find the high-level routines. In general it goes something like this:

enable graphics mode

define coordinate system

draw axes and labels

use SetPoint or Line command to draw curves in established coordinate system
 
Last edited: