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

  • Thread starter Thread starter taylrl3
  • Start date Start date
  • Tags Tags
    Graphs
Click For Summary
C++ does not have a built-in GUI library for graphing, but there are several options available for displaying data. Users can write data to a file and use external tools like Excel or Gnuplot for plotting. Alternatively, C++ can output data as an image file, although this requires either manual implementation or the use of a plotting library. Gnuplot is highlighted as a user-friendly option for generating publication-quality figures, with a C++ interface available. For those looking to implement graphics directly, enabling graphics mode and defining a coordinate system are essential steps, followed by drawing axes and using commands to plot points or lines. It's noted that searching for "plot" rather than "graph" yields better results in programming contexts, as "graph" often refers to mathematical structures.
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
 
Technology 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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
81
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K