ID      BlockSize Size      Blocks
-       511.991   241520288  471728
001     511.868   24152000   47184
0001    503.2     241520     480
00001   510.829   2415200    4728
000001  511.360   4782240    9352
0000001 486.935   120760     248
000     511.889   24103840   47088
0000    493.265   193360     392
00000   511.019   2367040    4632
000000  511.262   4830400    9448
0000000 483.4     96680      200

where I want

  • columns 3:4 used for plots
  • plot one graph from rows 2:6
  • another from 7:11

My start based on this blogpost

set terminal qt; 
plot "<(sed -n '2,6p' sandboxgp.data)" using 3:4 with lines; 
plot "<(sed -n '7,11p' sandboxgp.data)" using 3:4 with lines;

which gives you only one graph.

Marco's output

I put here logscale on y-axis for clarity where some code is based on this answer

set terminal qt; 
plot "<(sed -n '2,6p' sandbox_gp_pure.data)" using 3:4 with linespoints; 
replot "<(sed -n '7,11p' sandbox_gp_pure.data)" using 3:4 with linespoints; 
set logscale y 10; 
set xlabel "Size"; set ylabel "log(Blocks)"; 
set grid xtics ytics mxtics mytics lc rgb 'blue' lt 1, lc rgb 'red' lt 1; set mxtics; set mytics 5; 
set out;


enter image description here

  • Dear Masi it seems you changed the data that you presented in your plot. (different filenames from sandboxgp.data to sandbox_gp_pure.data). Please update always the data. Clarify, if you mind, even the output you want to create. From the ID I guess you have 2 datasets (lines 3-7 and 8-11) that you want to plot in different color. Is it true? – Hastur Nov 11 '15 at 8:48
  • No. I have changed nothing. The code to generate the thing is in the body of question and also the data is there. – Léo Léopold Hertz 준영 Nov 11 '15 at 11:20

Do not use the replot command, use the comma instead ,
Since in your script I see no apparent reasons to need the use the replot command, I suggest to use directly the comma , to separate the two curve to be plotted: plot sin(x), cos(x) for example.

Take it as a good habit but it is more, it is different in principle (see below).
You may find interesting to put \ as last character to split a single line (note that it requires that there are not blank spaces or other characters after). It makes the script more clean.

# ...
set style data linespoint   # To avoid to repeat it on each line of plot command
                            # Note below no spaces after the `\` 
plot "<(sed -n '2,6p'  sandbox_gp_pure.data)"  using 3:4  \
   , "<(sed -n '7,11p' sandbox_gp_pure.data)"  using 3:4 

With the replot command instead you plot again each curve already present on the graphic (rereading the data and performing again all the following operations) and only after you plot the new curve.

It is a good habit because using again your script tomorrow, you can have a general slow down of your work process when the files are many, huge or on a remote filesystem; when you perform long operations to process the data; when the points effectively plotted are many and you need to wait the graphical update of the window even more if you are working through an ssh -X connection...

Moreover in terminal as pdfcairo with

set terminal pdfcairo;  set output 'my.pdf' ;
  plot    sin(x) 
  replot  cos(x)
set output  ; set terminal qt # or whatever is your default terminal

you will obtain a 2 pages document, and a huger pdf file.

Note: You can use the every keyword without need to create subshells () and call external programs such as sed. If you cannot pre-order them with a sort, you can add smooth unique to plot the dataset with the linespoints style that loves entries ordered by the x coordinate.

plot "sandbox_gp_pure.data" every ::1::5   us 3:4 t "set 1" w linesp \
   , ''                     every ::6::10  us 3:4 t "set 2" w linesp

Or if you want them ordered

plot "sandbox_gp_pure.data" every ::1::5  u 3:4 smooth unique t "set 1" w linesp\
   , ''                     every ::6::10 u 3:4 smooth unique t "set 2" w linesp

Another advantage is the portability. It will work even where sed is not installed and should work even under Windows.
You can notice the shift in the line-number it's because it starts from 0.

The first datum in each block is numbered '0', as is the first block in the file.

The gnuplot help replot output:

The replot command without arguments repeats the last plot or splot command. This can be useful for viewing a plot with different set options, or when generating the same plot for several devices.

Arguments specified after a replot command will be added onto the last plot or splot command (with an implied ',' separator) before it is repeated.

  • Excellent answer about good coding with clear syntax! I did not know those details about computation. Excellent points about the portability at the end! Ex – Léo Léopold Hertz 준영 Nov 11 '15 at 11:58
  • 1
    To split the lines in that way allows you to comment (#) quickly one or more lines from the bottom of a plot block. Unfortunately it's not possible in the middle. Let's we honour my own nickname... You can define f(x,w,l1,l2)= ((w>=l1) && (w<=l2)) ?x : 1./0 and plot with plot "sandboxgp.data.orig" us 3:(f($4,$0,1,5) ) t "set 1" w linesp ... Useful when you need to plot a more complex function of your data. You can do selection on the values of x,y,z too of course... – Hastur Nov 11 '15 at 12:37

Use replot instead of plot the second graph. Then it will not overwrite the first graph.

# will plot one graph
gnuplot -p -e 'plot sin(x); plot cos(x)'

# will plot two graphs
gnuplot -p -e 'plot sin(x); replot cos(x)'
  • Excellent! Can you somehow avoid the duplicate entries like using 3:4 in both commands. I added the output of your plot to the body of the question. – Léo Léopold Hertz 준영 Jul 22 '15 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.