Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

prefix - 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'
 
question from:https://stackoverflow.com/questions/65863357/gnuplot-how-to-get-c-zero-space-for-numbers-1-999

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like a bug.

This is the workaround I came up it. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...