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

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

Tags:
  1. May 18, 2018 #1
    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!
     
  2. jcsd
  3. May 18, 2018 #2

    Mark44

    Staff: 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.
     
  4. May 18, 2018 #3

    DrClaude

    User Avatar

    Staff: Mentor

    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
     
  5. May 21, 2018 #4
    C++ has no concept of graphics, you need a library that does. The most reliable and widely used would probably be Boost.
     
  6. May 22, 2018 #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.
     
  7. May 22, 2018 #6

    jim mcnamara

    User Avatar

    Staff: 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.
     
  8. May 29, 2018 #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+.
     
  9. May 30, 2018 #8

    hilbert2

    User Avatar
    Science Advisor
    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 (Text):
    #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 (Text):
    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.
     
  10. May 31, 2018 #9

    jtbell

    User Avatar

    Staff: Mentor

    Or for the C++ purists among us:
    Code (C):

    #include <iostream>

    // yada yada...

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

    cout << x << '\t' << y << endl;
     
     
    Last edited: May 31, 2018
  11. May 31, 2018 #10

    FactChecker

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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.
     
  12. May 31, 2018 #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)
     
  13. Jun 1, 2018 #12
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted