Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Graphs from C++

  1. Jul 22, 2010 #1

    I have just started to learn C++ after a year programming with IDL. I realise 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.

  2. jcsd
  3. Jul 22, 2010 #2


    User Avatar
    Science Advisor
    Homework Helper

    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.
  4. Jul 23, 2010 #3
    Thanks! I had the feeling it couldnt produce graphs directly.
    How would I go writing the data out as an image file? Can I include axis etc like this?
  5. Jul 23, 2010 #4


    User Avatar
    Science Advisor
    Homework Helper

    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 [Broken]
    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: May 4, 2017
  6. Jul 23, 2010 #5
    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: Jul 23, 2010
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook