2

This thread here: Custom string in xticlabels

solved the question of customizing xticlabel strings.

Now, how do I sort the data from column 4 (for example) so that only rows containing certain strings in column 4 will be used to create the xticlabel? IOW, what is the proper format to do: (IF strcol(4) eq "Sunrise") plot 'datafile' u 4:2:( xticlabels( strcol(4).strcol(2) ) )

Community
  • 1
  • 1
Dan
  • 527
  • 1
  • 7
  • 25

1 Answers1

2

Given this datafile:

Sunrise cat 1
Sunset dog 2
Sunrise fish 3
waste space 4
blah blah 5
Sunrise label 6

we can plot it with this line:

plot 'test.dat' u 3:xticlabels(strcol(1) eq 'Sunrise'?strcol(1).strcol(2):'')

And it creates this plot:

enter image description here

Basically what I did there is I looked at the string in column 1, if it is "Sunrise", I concatenated it with the string in column 2. If it isn't "Sunrise", then I return an empty string to prevent a label from being placed there. This does however, place a major tic at the location of each data point. To avoid that, you can use the following:

plot 'test.dat' u 3:xticlabels(strcol(1) eq 'Sunrise'?strcol(1).strcol(2):NaN)

which produces this plot (I've tested on gnuplot 4.4.2 and 4.6.0):

enter image description here

It also issues a bunch of warnings about non-string labels, but I guess that's OK.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Thanks. Very useful. Now if I want to catch all the items in col 1 which contain "Sun", how is that done? I've found a suggestion of using "strcol(1)"[1:3] but can't seem to make it work here. Is awk more useful here? I'm not sure how to format that into the xticlabels setup. – Dan Feb 21 '13 at 10:12
  • 1
    @Dan -- I believe you can use `strstr` to check if a column contains a substring: `... using 3:xticlabels(strstr(strcol(1),'Sun')?strcol(1):NaN)` – mgilson Feb 21 '13 at 13:33
  • @mgilson-It works fine like this: > "" u 1:2:xticlabels(substr(strcol(4),1,3) eq 'Sun' ? strcol(4). ": " . strcol(2):NaN) but I hadn't realized the issue is more compicated: I'm plotting data where Sunrise and Sunset both occur in the same 24 hour period, so the second "Sun" always steps on the first. I cannot bend my head around how to concatenate the two occurrences into a single xtics string: :xticlabels(Sunrise:time . " " . Sunset: time) > 2011-10-03 06:39 Sunrise > 2011-10-03 18:17 Sunset – Dan Feb 26 '13 at 13:11
  • 1
    @Dan -- I'm not sure I understand. This new problem might require pre-processing of the file -- It's a bit too hard to tell. Maybe open a new followup question? – mgilson Feb 26 '13 at 14:17