Search code examples
c++plotgnuplot

Plotting multiple dataset in same gnuplot window


I have two data set (x,y1) and (x,y2) which I got from the result of computation and wrote those files in "data1.tmp" & "data2.tmp". I want to use this two data set to plot in Gnuplot.

#include <iostream>
#include <cstdlib>

int main()
{
    FILE* gnupipe1, *gnupipe2;
    
    const char* GnuCommands1[] = {"set title \"v vs x\"","plot \'data1.tmp\' with lines"};
    const char* GnuCommands2[] = {"set title \"y vs x\"","plot \'data2.tmp\' with lines"};

    gnupipe1 = _popen("gnuplot -persistent","w");
    gnupipe2 = _popen("gnuplot -persistent", "w");

    for (int i = 0; i < 2; i++)
    {
        fprintf(gnupipe1,"%s\n",GnuCommands1[i]);
        fprintf(gnupipe2,"%s\n", GnuCommands2[i]);
    }
    return 0;
}

Now when I run the program two window shows up plotting the data accurately.

How to plot multiple data set this way? say (x,y1) & (x,y2) in same window?


Solution

  • You are opening two different gnuplots, you don't need to do that.

    #include <iostream>
    #include <cstdlib>
    
    int main()
    {
        FILE* gnupipe1;
        
        const char* GnuCommands1[] = {"set title \"v vs x\"",
                     "plot \'data1.tmp\' with lines, \'data2.tmp\' with lines"};
    
        gnupipe1 = _popen("gnuplot -persistent","w");
    
        for (int i = 0; i < 2; i++)
            fprintf(gnupipe1,"%s\n",GnuCommands1[i]);
    
        return 0;
    }