plot - Set parameters in gnuplot using C++ source code -
i'm trying plot vectors using gnuplot c++ source code. read question how interface c++ gnuplot.
this source code:
int main() { char * commandsforgnuplot[] = {"set title \"trajectory_arm\"","plot 'data.temp' linespoints"}; double xvals[num_points] = {1.0, 2.0, 3.0, 4.0, 5.0}; double yvals[num_points] = {5.0 ,3.0, 1.0, 3.0, 5.0}; file * temp = fopen("data.temp", "w"); /*opens interface 1 can use send commands if typing * gnuplot command line. "the -persistent" keeps plot open after * c program terminates. */ file * gnuplotpipe = popen ("gnuplot -persistent", "w"); int i; (i=0; < num_points; i++) { fprintf(temp, "%lf %lf \n", xvals[i] , yvals[i]); //write data temporary file } (i=0; < num_commands; i++) { fprintf(gnuplotpipe, "%s \n", commandsforgnuplot[i]); //send commands gnuplot 1 one. } return 0; }
the problem is:
i have new vector of 5 points(a) , want have in same plot , xvals in respect of yvals. how should modify code? in case plot , xvals 2 different colors.
this complete source code formatted in c. have added third vector a
, , third instruction commandsforgnuplot
('' u 1:3 w lp
) plots vector a
respect xvals
. notice gnuplot draws each line different colors default.
#include <stdio.h> #include <stdlib.h> #define num_points 5 // each vector has 5 points int main() { double xvals[num_points] = {1.0, 2.0, 3.0, 4.0, 5.0}; double yvals[num_points] = {5.0 ,3.0, 1.0, 3.0, 5.0}; double avals[num_points] = {4.0 ,5.0, 3.0, 0.0, 1.0}; int i; //write xvals, yvals, , avals temporary file file * temp = fopen("data.temp", "w"); for(i=0; < num_points; i++) fprintf(temp, "%lf %lf %lf\n", xvals[i] , yvals[i], avals[i]); fclose(temp); // close file if won't used anymore // open interface gnuplot. option -p lets plot windows survive after program exits file * gnuplotpipe = popen ("gnuplot -p", "w"); // send commands gnuplot 1 one. fprintf(gnuplotpipe, "%s \n", "set title 'trajectory\\_arm'"); // command 0: define graph title fprintf(gnuplotpipe, "%s \n", "plot 'data.temp' u 1:2 w lp"); // command 1: plot yvals vs xvals, , avals vs xvals fflush(gnuplotpipe); // plots data current commands // obtained plot of data.temp // now, place instruction pause program until key typed getchar(); // add plot current graph fprintf(gnuplotpipe, "%s \n", "replot 'data.temp' u 1:3 w lp"); pclose(gnuplotpipe); // close gnuplot if won't used anymore return 0; }
Comments
Post a Comment