# Is it possible to make graphs using C++?

• C/++/#
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!

## Answers and Replies

Mark44
Mentor
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.

JorgeM
DrClaude
Mentor
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

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.

JorgeM
.Scott
Homework Helper
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.

JorgeM
jim mcnamara
Mentor
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.

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+.

JorgeM
hilbert2
Gold Member
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.

jtbell
Mentor
Or for the C++ purists among us:
Code:
#include <iostream>

cout << x << ' ' << y << endl;
I've always preferred tab-delimited output for exporting to external software like Excel:
Code:
cout << x << '\t' << y << endl;

Last edited:
FactChecker
Gold Member
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.

JorgeM
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)

JorgeM and FactChecker
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 gonna also use it for Complex Variable and let's see what happens.

rootone
Good luck.