Siddhant
Siddhant

Reputation: 2525

How do I plot multiple Y values for a single X value in a single Gnuplot window?

I'm using C++ to plot graphs using Gnuplot. In the C++ program, I popen() a Gnuplot process file, and keep writing to it to plot my graphs. Specifically, I write "plot '-' using 1:2 with points", and then continue with writing the X-Y coordinates. For just two columns, it works fine.

Now I have modified my program to generate more than 2 columns. I now have data in the following format

X, Y1, Y2, Y3, Y4, Y5

So every second, it outputs one line (for example "1 2 3 4 5 6", where 1 is the X coordinate, and the rest of the values are Y coordinates for different curves). I cannot figure out how to fit all the curves in one single window.

If I do something like the following,

set xrange[0:5]
set yrange[0:10]
plot '-' using 1:2 with lines, '-' using 1:3 with lines
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7

it gives me an error which says

"warning: Skipping data file with no valid points"

and also requires pressing "e" twice to indicate the end of data.

Maybe I'm missing something tiny here.

Thanks!

Upvotes: 0

Views: 3475

Answers (1)

xscott
xscott

Reputation: 2420

I can think of two ways, but they both involve iterating through your data multiple (5) times. First, multiple plots can be separated by commas like such:

plot '-', '-', '-', '-', '-'

You'll then fprintf(...) an 'e' after each set of data.

Additionally, since you're just plotting points (not lines), you could just keep sending more data:

fprintf(gnuplot, "plot '-' with points\n");
for (int yy = 1; yy <= 5; yy++) {
    for (int row = 0; row < len; row++) {
         fprintf(gnuplot, "%lf %lf\n", data[row][0], data[row][yy]);
    }
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);

Upvotes: 3

Related Questions