Home > OS >  Gnuplot and C - plot different symbols/colors
Gnuplot and C - plot different symbols/colors

Time:03-10

I would like to plot different points with different colors and symbols. Here is my Code in "C" :

float x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
float y[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

FILE *gnuplot = popen("gnuplot", "w");

fprintf(gnuplot, "plot '-' pt 7 pointsize 2 lc \"blue\" notitle \n");

for (int i = 0; i < 6;   i) {

    fprintf(gnuplot,"%f %f\n", x[i], y[i]);
}

fprintf(gnuplot, "plot '-' pt 5 pointsize 2 lc \"red\" notitle \n");

for (int i = 6; i < 12;   i) {

    fprintf(gnuplot,"%f %f\n", x[i], y[i]);
}

fprintf(gnuplot, "e\n");
fflush(gnuplot);
getch();
fclose(gnuplot);

Unfortunately, all points have the same type and same color. What is my mistake? Thanks in advance!

Edit:

enter image description here

Addition: (as mentioned in the comments)

Since gnuplot 5.0 you have datablocks which are including the data as well into the plotting code, check help inline. The advantage is that if you want to make plots of several columns you don't have to provide the data multiple times or split it into sequential 2 column data. The following examples will all result in the same plot:

Code:

$Data1 <<EOD
0    0
1    1
2    2
3    3
4    4
5    5
EOD

$Data1 <<EOD
6    6
7    7
8    8
9    9
10   10
11   11
EOD

plot $Data1 u 1:2 w p pt 7, \
     $Data2 u 1:2 w p pt 5
$Data <<EOD
0    0    6    6
1    1    7    7
2    2    8    8
3    3    9    9
4    4    10   10
5    5    11   11
EOD

plot $Data u 1:2 w p pt 7, \
        '' u 3:4 w p pt 5
plot '-' u 1:2 w p pt 7, \
     '-' u 1:2 w p pt 5,
0    0
1    1
2    2
3    3
4    4
5    5
e
6    6
7    7
8    8
9    9
10   10
11   11
e
plot '-' u 1:2 w p pt 7, \
     '-' u 3:4 w p pt 5,
0    0    6    6
1    1    7    7
2    2    8    8
3    3    9    9
4    4    10   10
5    5    11   11
e
0    0    6    6
1    1    7    7
2    2    8    8
3    3    9    9
4    4    10   10
5    5    11   11
e
  • Related