Is it possible to make graphs using C++?

  • C/C++
  • Thread starter JorgeM
  • Start date
  • Tags
    Graphs
In summary: For plotting functions, I've found that MATLAB is the best all-around package. However, there are other packages that can do basic plotting as well, such as Octave and R.If you want to do more complicated plotting, you'll likely need to look into a graphics library. There are many on the internet, such as OpenGL or DirectX.
  • #1
JorgeM
30
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
  • #2
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 JorgeM
  • #3
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 JorgeM and FactChecker
  • #4
C++ has no concept of graphics, you need a library that does. The most reliable and widely used would probably be Boost.
 
  • Like
Likes JorgeM
  • #5
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 JorgeM
  • #6
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 JorgeM
  • #7
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 JorgeM
  • #8
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.
 
  • #9
Or for the C++ purists among us:
Code:
#include <iostream>

// yada yada...

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:
  • #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 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 JorgeM and 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 rootone
  • #14
Good luck.
 

1. Can you make graphs using C++?

Yes, it is possible to make graphs using C++. C++ has various libraries and functions that allow for the creation of graphs and charts.

2. What libraries are available for creating graphs in C++?

Some popular libraries for creating graphs in C++ include Boost Graph Library, Graphviz, and Gnuplot.

3. How do I create a simple graph in C++?

To create a simple graph in C++, you can use the plot function from the Gnuplot library. This function takes in the data points and plots them on a graph.

4. Are there any tutorials or resources for learning how to make graphs in C++?

Yes, there are many tutorials and resources available online for learning how to make graphs in C++. Some popular ones include the official documentation for the libraries mentioned earlier, as well as various tutorials on YouTube and coding websites such as GeeksforGeeks.

5. Can I customize the appearance of my graphs in C++?

Yes, most libraries for creating graphs in C++ allow for customization of the appearance of the graph. This includes changing the colors, labels, and other design elements.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
123
  • Programming and Computer Science
Replies
2
Views
619
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
276
Replies
9
Views
945
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
42
Views
6K
  • Programming and Computer Science
Replies
32
Views
2K
Back
Top