2

I want to create scatter plot of a file that looks like:

counter  N   x  y   
  1     200  50 50  
  2     200  46 46  
  3     200  56 56  
  4     200  36 36
  5     200  56 56

There are 240 lines in this file. The N is incremented by 200 every 30 lines. So, when I plot the numbers I want to create a scatter plot of x, y values vs. counter. Here is my code:

plot "file" using 1:3 title "hb" with points pt 2 ps 1 lc rgb "red", \
     "file" using 1:4 title "ls" with points pt 3 ps 1 lc rgb "blue"

As a result my x-axis has the range [1,240].

The question is that I want the label of my x-axis to contain the values from the second column, and I want them to be printed after every 30 points.

So, I want my x-axis label to be customized as: [200,400,600,800,1000,1200,1400,1600] where they each have 30 points in between.

I actually searched for this question before, found the solution and solved it. So, I know there is an answer somewhere. But apparently I lost my code. I have been searching for the old post for an hour now but could not find it.

Can anyone help me with using customized labels here?

begumgenc
  • 393
  • 1
  • 2
  • 12

2 Answers2

3

I'm not sure how to generate xtics from the data in gnuplot, so I'd use bash to generate them for me:

#! /bin/bash
xtics='('$(cut -d' ' -f1,2 file | sort -nuk2 | sed 's/\(.*\) \(.*\)/\2 \1/;s/^/"/;s/ /" /;s/$/,\\/')$'\n)'
gnuplot <<EOF
set term png
set output '1.png'
set xtics $xtics
plot "file" using 1:3 title "hb" with points pt 2 ps 1 lc rgb "red", \
         "" using 1:4 title "ls" with points pt 3 ps 1 lc rgb "blue"
EOF

On a randomly generated input, it gives this output:

generated graph

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Thank you very much, I accepted the other reply as the answer as it is an easier way to do it. I would have marked yours as the answer otherwise. – begumgenc Dec 04 '18 at 14:00
3

You can evaluate any expression in xticlabel to give a string or an invalid value. In order to set labels only at certain values of column 1, you can use

plot "file" using 1:3:xtic(int($1)%30 == 0 ? strcol(2) : 1/0) title "hb" pt 2 lc rgb "red", \
     "" using 1:4 title "ls" pt 3 lc rgb "blue"

Thr expression xtic(int($1)%30 == 0 ? strcol(2) : 1/0) places the string value of column 2 when the value in column 1 is a multiple of 30. All other values are skipped, because 1/0 is an invalid value.

Christoph
  • 47,569
  • 8
  • 87
  • 187