#368 Unit conversion in coordinates

None
closed-works-for-me
nobody
coordinates (1)
6
9 hours ago
2013-07-21
Drew
No

Thank you all for an excellent software product.

Various lengths in gnuplot are given in different units, e.g. screen or graph, while others, like the offset length of a key box are given in characters.

Currently, to place for example a key box right-justified at a certain position, the length of a character in a terminal has to be estimated through trial-and-error, but the terminal driver knows the value to, for example, determine the size of the bounding box of a key.

(I realize there is a keyword to right-justify the key box, however due to the inaccuracy of font rendering, this does not always work as intended, which is why the width option exists. Either way, it is beneficial to expose the dimensions of the box to the user for effects like gradient backgrounds and drop shadows.)

Please see example script below for an application and note the character width and height variables chrw and chrh and the key length variable keylen.

For this application, as well as working around labels and other character items, it would be very beneficial if the relationship between units in a terminal were exposed to the user, either as

1) conversion factors to allow arithmetic of the form, e.g.

... at graph 1-10*CHAR2GRAPH-5*FIRST2GRAPH, ...

or

2) units, e.g.

... at 1graph-10char-5first

I suppose option (1) would be easier for you to implement, as it does not change the syntax.

Thank you for your efforts and best wishes.

Example script to be run in linux:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/gnuplot -persist
pwid=5
phgt=3.5
pfnt=24

set term postscript enhanced eps color dashed size pwid,phgt "Helvetica" pfnt
set output "tricks-bkey.eps"

xl=0; xh=20; yl=-1; yh=1;
xofs=0.5; yofs=2.0;
rmar=1.5; lmar=7.75-yofs;
tmar=0.5; bmar=3.25-xofs;

addhgt=0.5

screencol1="#68c068"
screencol2="#ffffff"
graphcol1="#e6e680"
graphcol2="#ffffff"
keycolshade="#aaaaaa"
keycol1="#c8e0c8"
keycol2="#f5f8f5"

set xrange [xl:xh]
set yrange [yl:yh]

## tic length, character width, character height (gnuplot's estimate)
## (reverse engineered for term postscript eps Helvetica)
ticl=0.0075*5./pwid
chrw=0.02000*5./24.*pfnt/pwid
chrh=0.095/2*3.5/24.*pfnt/phgt

## key position, epsilon for shadow, width and height
kmag=0.05*pfnt/24; kx=1-rmar*chrw-kmag*phgt/pwid; ky=1-tmar*chrh-kmag/2
yeps=kmag/2; xeps=yeps*phgt/pwid;
## adapt these three to your key items
sampl=2;

keys1='y=sin(x)*exp(-x/10)'
keys2='Data set 2'
keys=2+addhgt

keyl1=strlen(keys1)
keyl2=strlen(keys2)
max(x,y)=(x>y)?(x):(y)
keylen=max(keyl1, keyl2)

delwidth=0.225*keylen;
kw=(keylen+sampl+2-delwidth)*chrw+ticl;
kh=keys*chrh

f(x) = exp(-x/10.0)*sin(x)
g(x) = exp(-x/10.0)
h(x) = -exp(-x/10.0)

set sample 30
set table 'test.dat'
plot f(x)+(rand(0)-0.5)*0.2
unset table

set sample 128
set isosamples 2,128

set table 'background.dat'
splot (y-yl)/(yh-yl)
unset table

set multiplot

set rmargin 0
set lmargin 0
set tmargin 0
set bmargin 0

unset key
unset colorbox
unset border
unset xtics
unset ytics

set palette defined (0 screencol1, 1 screencol2)
plot 'background.dat' w ima

set rmargin rmar
set lmargin lmar
set tmargin tmar
set bmargin bmar

set palette defined (0 graphcol1, 1 graphcol2)
plot 'background.dat' w ima

set rmargin (1-kx-xeps)/chrw
set lmargin (kx+xeps-kw)/chrw
set tmargin (1-ky+yeps)/chrh
set bmargin (ky-yeps-kh)/chrh

set palette defined (0 keycolshade, 1 keycolshade)
plot 'background.dat' w ima

set rmargin (1-kx)/chrw
set lmargin (kx-kw)/chrw
set tmargin (1-ky)/chrh
set bmargin (ky-kh)/chrh

set palette defined (0 keycol1, 1 keycol2)
plot 'background.dat' w ima

set rmargin rmar
set lmargin lmar
set tmargin tmar
set bmargin bmar

set key at screen kx,ky reverse Left samplen sampl width -delwidth height addhgt box

set xtics
set ytics
set border 1+2+4+8
set xlabel 'Time [s]' offset 0,xofs
set ylabel 'Position [m]' offset yofs,0

plot \
  f(x) w l lt 3 lw 2 t keys1 \
, g(x) w l lt rgb "#008800" t '' \
, h(x) w l lt rgb "#008800" t '' \
, 'test.dat' u 1:2:($2-rand(0)*0.1-0.05):($2+rand(0)*0.1+0.05) t keys2 w errorb pt 13 lt 1 \

unset multiplot

!/bin/rm -f test.dat background.dat
!gv -scale=3 tricks-bkey.eps &

Discussion

  • Ethan Merritt
    Ethan Merritt
    2013-08-07

    If I understand correctly what you are asking for, the necessary information is already exported to the user via the variables GPVAL_*.
    See "help GPVAL"
    and "show var GPVAL"

    The font information is something else again. Gnuplot itself doesn't really know enough about font metrics to export this. It relies on individual drivers to do whatever is necessary for font spacing, etc. The closest thing to a consistent font size variable is the per-terminal values term->v_char and term->h_char. These are not currently exported to the user, but I suppose they could be. The problem is that they change dynamically as text is processed, so I don't think querying them outside of a text producing command would necessary do what you want.

     
  • Ethan Merritt
    Ethan Merritt
    2013-08-28

    • status: open --> closed-works-for-me
    • Group: -->