Skip to main content

Linking GNUplot to C/C++

How to get an output graph instead of an output datafile!!!!

 

C interface

First of all, two things to remember:

the set of N data (x,y) to plot should be in two arrays, one containing the x values and one with the associated y values :o) 

 h1 is the name of the GNUplot active windows called by C

Let's now compare the GNUplot commands with the C code:

 GNUplot

C
   

gnuplot

#include "gnuplot_i.h"

gnuplot_ctrl * h1;

h1 = gnuplot_init();

   

set xrange [0:6.28]

set yrange [0:3.14]

set xlabel 'theta'

set ylabel 'phi'

gnuplot_set_xrange(h1, 0, 6.28);

gnuplot_set_yrange(h1, 0, 3.14);

gnuplot_set_xlabel(h1, "theta");

gnuplot_set_ylabel(h1, "phi");

   

plot "filename" using 1:2 title "myplot"

gnuplot_setstyle(h1, "points") ;

gnuplot_plot_xy(h1, array1, array2, N, "myplot");

   

plot "filename" using 1:2 title "myplot" w l

gnuplot_resetplot(h1);

gnuplot_setstyle(h1, "lines") ;

gnuplot_plot_xy(h1, array1, array2, N, "myplot");

   

set terminal postscript

set output 'output.ps'

replot

gnuplot_hardcopy(h1, output);

   

quit

gnuplot_close(h1) ;


Files

gnuplot_i.h

gnuplot_i.c 

gnuplot_i_example.c

Credits

This code was based on the C interface to gnuplot written by Nicolas Devillard.

 

 

C++ interface

This is an object oriented interface.

Files

gnuplot_i++.tgz (USA)

gnuplot_i++.tgz (UK)

Credits

The example here uses a C++ interface to gnuplot written by Rajarshi Guha, based on the code written by Nicolas Devillard.