gnuplotprefix

gnuplot: How to get %c zero space for numbers 1-999?


To my understanding there should be one empty space between numbers and units.

However, in gnuplot the prefix %c (see help format specifiers) for numbers from 1-999 apparently seems to be ' ' instead of ''. So, in the example plot below neither the xtic labels nor the ytic labels are all correct. Either you have some tic labels with zero space or with two spaces, but not all with one. It's a detail and maybe some people won't even notice, but if possible I would prefer to do it the correct way.

Quite some time ago I placed a bug report, but no response so far. Is there maybe an immediate workaround?

Code:

### wrong prefix for %c 1-999
reset session

set size ratio -1
set logscale xy

set format x "%.0s%cΩ"    # no  space
set format y "%.0s %cΩ"   # one space
set xrange [1e-3:1e12]
set grid x, y
plot x
### end of code

Result:

enter image description here

Addition:

Based on the answer of @maij pointing to the source code, here is a gnuplot attempt to "fix" this, which should be easily transferred to C.

Code:

### gnuplot "fix" for prefix for %c 1-999

prefix(power) = "yzafpnum kMGTPEZY"[(power+24)/3+1 : (power+24)/3 + sgn(abs(power))]

do for [power=-24:24:3] {
    print sprintf("% 3g  '%s'", power, prefix(power))
}
### end of code

Result:

-24  'y'
-21  'z'
-18  'a'
-15  'f'
-12  'p'
 -9  'n'
 -6  'u'
 -3  'm'
  0  ''   # zero length
  3  'k'
  6  'M'
  9  'G'
 12  'T'
 15  'P'
 18  'E'
 21  'Z'
 24  'Y'
 

Solution

  • It looks like a bug.

    This is the workaround I came up with. With this, there is always one space between the number and the units.

    You can tell gnuplot where to set each tick mark individually and what each label should be, with set xtics add. The function gprintf can format a number using gnuplot specifiers.

    Since we already know what value each tick should have, it's easy to set them with a loop.

    # Function to choose the right label format.
    f(x) = (x < 1000 && x >= 1) ? gprintf("%.0f Ω", x) : gprintf("%.0s %cΩ", x)
    
    # Loop to set each tick mark and label.
    do for [i=-2:12:2] {
        set xtics add (f(10**i) 10**i)
        set ytics add (f(10**i) 10**i)
    }
    
    set size ratio -1
    set logscale xy
    set xrange [1e-3:1e12]
    set grid x, y
    
    plot x