Search code examples
cgnuplot

Gnuplot and C - plot different symbols/colors


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:

Result of the code

All points are blue and have the pointtype 7 and the pointsize 2. The second command line to change the style has no effect:

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

Solution

  • Your code is like the following:

    plot '-' pt 7 ps 2 lc "blue" notitle
    0    0
    1    1
    2    2
    3    3
    4    4
    5    5
    plot '-' pt 6 ps 2 lc "red" notitle
    6    6
    7    7
    8    8
    9    9
    10   10
    11   11
    e
    
    

    For inline data, i.e. special filename '-' the data is ended by a line e. Check help special-filenames.

    So, your code will:

    • plot x from 0 to 5 with blue filled circles
    • the line plot '-' pt 6 ps 2 lc "red" notitle will be interpreted as data line because inline data has not yet been ended by a line e, hence, this line will be ignored because it does not contain numerical data
    • plot x from 6 to 11 with blue filled circles

    But it seems you want to have the following:

    Code:

    plot '-' pt 7 ps 2 lc "blue" notitle, \
         '-' pt 6 ps 2 lc "red"  notitle
    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
    

    Result:

    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