SvckG
SvckG

Reputation: 55

How to use gnuplot from within C program to plot graph

I am trying to plot a graph from within C program (Windows 7) I have maintained an array of graph points,say x[] and y1[],y2[] and y3[]. I want to plot multiple y points for fixed x points. How can I use gnuplot from within my program to plot the graph?

Upvotes: 1

Views: 7797

Answers (1)

ca_san
ca_san

Reputation: 193

Not the most elegant solution, but this should work:

int main(int argc, char **argv){
    FILE * temp = fopen("data.temp", "w");
    FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
    for(Iterate over your array so that you create another exact temporal array){

        float a, b, c;

        x = something;
        y1 = something;
        y2 = something;
        y3 = something;

        fprintf(temp, "%d %d %d %d \n", x, y1, y2, y3);
    }

    fprintf(gnuplotPipe, "(here whatever you want to tell gnuplot to do) i.e plot 'data.temp' using 1:2 with lines, etc");    

    return 0;
}

Alternatively you can do this:

int plot(){
    system("gnuplot -p -e \"(Here put whatever you would put in gnuplot as if you were ploting manually)\"");
    return 0;
}

Upvotes: 2

Related Questions