Is it possible to make graphs using C++?

  • Context: C/C++ 
  • Thread starter Thread starter JorgeM
  • Start date Start date
  • Tags Tags
    Graphs
Click For Summary

Discussion Overview

The discussion revolves around the feasibility of creating graphs using C and C++. Participants explore various methods, libraries, and approaches for graphing functions, including numerical methods, data export, and integration with other applications.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants express uncertainty about the capability of C/C++ for graphing, noting that these languages do not inherently support graphics beyond simple character-based representations.
  • Others suggest using various graphics libraries such as OpenGL, DirectX, SDL, and Boost to facilitate graphing.
  • A few participants recommend higher-level libraries like matplotlib-cpp and Qwt for easier plotting.
  • One participant mentions generating SVG files or BMP files for plotting, indicating that SVG files are easier to work with due to their text format.
  • Another participant discusses the possibility of exporting data to applications like Excel or MATLAB for graphing purposes.
  • Some suggest using C++ in conjunction with other programming languages like Python or R for a more streamlined graphing process.
  • One participant shares their experience of successfully creating a 2D graph plotter using Allegro, highlighting its utility for complex plotting tasks.

Areas of Agreement / Disagreement

Participants generally agree that C/C++ alone are not suitable for graphing without additional libraries. However, there are multiple competing views on which libraries or methods are best suited for the task, and the discussion remains unresolved regarding the optimal approach.

Contextual Notes

Some limitations include the need for external libraries, the learning curve associated with them, and the dependency on specific file formats for data export and import.

Who May Find This Useful

This discussion may be useful for programmers interested in graphing data using C/C++, those exploring different libraries for graphical representation, and individuals looking for methods to integrate C/C++ with other graphing applications.

JorgeM
Messages
30
Reaction score
6
I am trying to graph some functions but I am not sure about if it is possible using C/C++
I use some numerical metods trying to interpolate data of much complex functions and get the graph (for example the Riemman Dseta function). But I am not even sure If there does exist one specific header or something like that that may help me.
Or if necesary how to export data or run other aplication (Excel, in case) that could help me usinng the c++. (That the c++ executable file opened excel and send data to make the graph)
I am quite confused about it.
Thanks a lot!
 
Technology news on Phys.org
Neither C nor C++ by themselves can be used to make graphs, other than very simple character-based graphs, which is almost certainly not what you want. However, there are lots of graphics libraries out there that you can use to generate images such as those you're trying to do. Here's a link to a page that discusses graphics programming in C/C++ using OpenGL -- https://www.cprogramming.com/graphics-programming.html

Other libraries include DirectX, SDL, and many more. If you do a web search for "c++ graphics libraries" you'll get lots of hits.
 
  • Like
Likes   Reactions: JorgeM
Mark44 said:
Neither C nor C++ by themselves can be used to make graphs, other than very simple character-based graphs, which is almost certainly not what you want. However, there are lots of graphics libraries out there that you can use to generate images such as those you're trying to do. Here's a link to a page that discusses graphics programming in C/C++ using OpenGL -- https://www.cprogramming.com/graphics-programming.html

Other libraries include DirectX, SDL, and many more. If you do a web search for "c++ graphics libraries" you'll get lots of hits.
These are nice suggestions, but they might be too low-level if one simply wants to plot some figures. I would suggest something like https://github.com/lava/matplotlib-cpp or http://qwt.sourceforge.net
 
  • Like
Likes   Reactions: JorgeM and FactChecker
C++ has no concept of graphics, you need a library that does. The most reliable and widely used would probably be Boost.
 
  • Like
Likes   Reactions: JorgeM
Depending on the situation, I often generate *.svg files and then open them up with a browser.
In one case, when I wanted to plot out diagnostic data, I dumped data directly to a BMP file.

But the SVG file is just text - so you'll likely find it easier to work with.

If you have MatLab available, you can output text to an *.m file and open it in MatLab.
 
  • Like
Likes   Reactions: JorgeM
Basic answer for a beginning programmer: C and C++ are not meant for graphing. Anything that is an add-on like boost will work but there is a steep learning curve.

However. There are lots of packages and applications that are good at graphing and all have some learning curve. Pick one you know. @.Scott gave you an example - but it means that you have to know a lot about the svg file format. Matlab, Maple, and python have plotting capabilities. If you already know how to develop i with one of them , choose go there. Otherwise create your dataset as a csv file and feed it to excel. Which you seem to know.

https://en.wikipedia.org/wiki/Comma-separated_values - examples and rules near the bottom.
 
  • Like
Likes   Reactions: JorgeM
Of course it's possible with C++. You just need some library for that. For example VTK: www.vtk.org can do both 2D and 3D charting and much more (such as volumetric visualizations and so on). Here is an example of a program that uses it for 3D charting: https://compphys.go.ro/solving-poisson-equation/ I'll post soon on GitHub an example of how can be used for 2D charting, with a blog post describing it.

You can also implement it yourself at some lower level, but I would not recommend it. Here is a project that does that: https://compphys.go.ro/the-numerical-renormalization-group/ using GDI+.
 
  • Like
Likes   Reactions: JorgeM
The simplest way to make graphs with data calculated by a C language code is to make the program print the data to standard output as a two-column data point set, and then import the results to some graphing program (Excel or other).

For instance, if I want to do this for the function ##f(x) = x^2## on the interval ##x\in [0,1]##, the code could be like

Code:
#include <stdio.h>

main()
{
double x,y;

for(int i=0; i<=20; i++) {
x = (double)i/20.0;
y = x*x;
printf("%f %f\n",x,y);
}
}

If this is compiled in a Windows system to an executable "x-squared.exe", you can then run it on command line with

Code:
c:\x-squared > output.txt

and then the file "output.txt", where the standard output was directed, can be imported to Excel, Origin Pro, Grace or some other graphing application.
 
Or for the C++ purists among us:
Code:
#include <iostream>

// yada yada...

count << x << ' ' << y << endl;
I've always preferred tab-delimited output for exporting to external software like Excel:
Code:
count << x << '\t' << y << endl;
 
Last edited:
  • #10
This is an example of a problem where the high-level scripting languages like Perl, Python, etc. are good. You can make a sequence of programs to do individual steps, each in a computer language that is best suited for each step and use the high-level scripting language to run and control them. For instance, you can use C++ to generate a data file of data in ASCII format. Then you can use a plotting package like R to read the data and create the graphs. A top-level Perl program can run the C++ program, make an R script for the plot, and run the R script in R. It can rename any files, do pre, post, or intermediate processing in between the lower level tasks.
 
  • Like
Likes   Reactions: JorgeM
  • #11
The usual way to approach this is to look for existing libraries that do whatever graphics you need.
Write your C or ++ code to make best use of the functions that library provides.
There are tons of graphic libraries many of which are open source, (so you can fiddle with them if they are not exactly ideal for your needs)
 
  • Like
Likes   Reactions: JorgeM and FactChecker
  • #12
  • Like
Likes   Reactions: FactChecker
  • #13
Hello Everyone!
At the end, I made a 2D graph ploter using Allegro for my C++ app.
It was not the easiest way but Incredibly was useful for the thing that I was trying to plot, even more than 3 function's graphs if I wanted(With both axis at different scales and you can browser all along the graph , even you can zoom it if you want.
Actually I am trying to make a reliable 3D parametricplot app in a similar way, but in fact is not that easy.
Thanks a lot for your opinions.
Pd. I am going to also use it for Complex Variable and let's see what happens.
 
  • Like
Likes   Reactions: rootone
  • #14
Good luck.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
81
Views
8K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K