steve_bank
Diabetic retinopathy and poor eyesight. Typos ...
A basic Gnuplot class. Titles and axis labels can be added. An adventurous person looking for a project could expand it into a C++ graphics package.
Gnuplot has to be installed but does not have to be running.
With extents >0 the plot is to the extents of the data, < 1 and plots are to the specified limits, a zoom function.
I found it easier to use C++ strings and use the .c_str() attribute when needed for C functions.
Gnuplot has to be installed but does not have to be running.
With extents >0 the plot is to the extents of the data, < 1 and plots are to the specified limits, a zoom function.
I found it easier to use C++ strings and use the .c_str() attribute when needed for C functions.
Code:
class GNUPLOT{
public:
int n = 1,extents = 1;
string path = "",plot_file =" ",data_file = "";
double xlo = 0,xhi = 1,ylo = -1, yhi = 1;
double *x, *y;
int plot(void);
};
int GNUPLOT::plot(void){
int i;
string fname = path+data_file;
FILE *p = fopen(fname.c_str(),"w");
if(!p){cout<<"siog file"<<endl;return 1;}
for(i=0;i<n;i++)fprintf(p,"%20.15f \t%20.15f\n",x[i],y[i]);
fclose(p);
fname = path+plot_file;
p = fopen(fname.c_str(),"w");
if(!p){cout<<"plot file"<<endl;return 1;}
fprintf(p,"set term windows background rgb 'white'\n");
fprintf(p,"reset\n");
if(extents){
fprintf(p,"set xrange[*:*]\n");
fprintf(p,"set yrange[*:*]\n");
}
else{
fprintf(p,"set xrange[%f:%f]\n",xlo,xhi);
fprintf(p,"set yrange[%f:%f]\n",ylo,yhi);
}
fname = path + data_file;
fprintf(p,"set grid lt 1 lw 1 lc rgb 'black' dashtype solid\n");
fprintf(p,"plot '%s' using 1:2 with lines ls 4 lt -1 lw 3\n",fname.c_str());
fprintf(p,"show grid\n");
fclose(p);
fname = path+plot_file;
system(fname.c_str());
return 0;
}
void plot_test(void){
GNUPLOT gp;
int i, n = 100;
double t[n],y[n],dt = 1./(n-1);
for(i=0;i<n;i++){
t[i] = i*dt;
y[i] = sin(2*_PI*1*t[i]);
}
gp.n = n; gp.extents = 1;
gp.path = "c:\\gnuplot\\data\\";
gp.plot_file = "sig.plt";
gp.data_file = "sig.dat";
gp.xlo = 0;gp.xhi = .4;gp.ylo = 0;gp.yhi = 1;
gp.x = t; gp.y = y;
gp.plot();
}