ifffam
ifffam

Reputation: 175

Conditional tics format: one decimal if >1000 but no decimals if <=1000

When plotting tics, the usual way to get 1k rather than 1000 is

    set format y '%.s%c'

Doing so, if I've got values greater than 1000 (but always lower than 2000), the format always renders 1k. If I set

    set format y '%.1s%c'

then for numbers smaller than 1000, I always get a decimal which I don't like since my values are almost multiple of 100 (I would get 100.0, 200.0 etc).

I'd like to get values in this format: 100, 200, ... ,900, 1k, 1.1k, etc. Any idea on how to get this mixed format? That is, without decimals if <1000 and with one decimal if >1000 (for 1000 itself, I'd prefer 1k but 1.0k is ok too).

Upvotes: 2

Views: 48

Answers (1)

theozh
theozh

Reputation: 26123

Although it would be nice, as far as I know, there is no "conditional formatting" for tics in gnuplot, i.e. something like:

set ytics format (y>=1000 ? "%.1s%c" : "%g")

So, I guess you have to set the tics "manually", which is at the cost of losing autoscale. However, if you insist on autoscale you could maybe create some cumbersome ways with multiplot and its disadvantages.

With adding another condition you could even trim your 1.0 k to 1 k.

Script:

### customized ytic labels
reset session

set xrange[0:2000]
set for [i=0:2000:200] ytics ((i>=1000 ? sprintf("%.1f k",i/1000.) : sprintf("%g",i)) i)

plot x
### end of script

Result:

enter image description here

Upvotes: 2

Related Questions