Gnuplot:存储绘图区域尺寸以备后用 [英] Gnuplot: Store plot area dimensions for later use
问题描述
是否可以在gnuplot中存储绘图空间的尺寸?我不是说整个画布,而是绘图框中的区域.
Is it possible to store the dimensions of the plotting space in gnuplot? I don't mean the whole canvas, rather, the area within the plotting frame.
推荐答案
在plot
命令之后,可以使用几个gnuplot定义的变量GPVAL_*
(类型show variables all
)来查看值.其中一些可用于获取上一个图的边距:
After a plot
command several gnuplot-defined variables GPVAL_*
are available (type show variables all
) to see the values. Some of them can be used to get the margins of the previous plot:
plot sin(x)
TSCALE = 1.0
LMARGIN = TSCALE*GPVAL_TERM_XMIN/(1.0*GPVAL_TERM_XSIZE)
RMARGIN = TSCALE*GPVAL_TERM_XMAX/(1.0*GPVAL_TERM_XSIZE)
TMARGIN = TSCALE*GPVAL_TERM_YMAX/(1.0*GPVAL_TERM_YSIZE)
BMARGIN = TSCALE*GPVAL_TERM_YMIN/(1.0*GPVAL_TERM_YSIZE)
要在以后恢复页边距,请使用
To restore the margins later, use
set lmargin at screen LMARGIN
set rmargin at screen RMARGIN
set tmargin at screen TMARGIN
set bmargin at screen BMARGIN
TSCALE
是一个与终端相关的因素,因为GPVAL_TERM_*MIN
和GPVAL_TERM_*MAX
在内部通过过采样因子进行缩放,但是GPVAL_TERM_*SIZE
的值不是(至少在4.6.0、4.6中.3和4.7(2013-09-23).对于pdfcairo
,该值必须是20
,而对于wxt
,它的值是1
.对于不同终端的比较完整的检查是:
TSCALE
is a factor, which is terminal-dependent, because the GPVAL_TERM_*MIN
and GPVAL_TERM_*MAX
are scaled internally by an oversampling factor, but the GPVAL_TERM_*SIZE
values are not (at least with 4.6.0, 4.6.3 and 4.7 (2013-09-23)). For pdfcairo
this value must be 20
, while for wxt
it is 1
. A rather complete check for different terminals is:
if (GPVAL_TERM eq 'pdfcairo' || \
GPVAL_TERM eq 'cairolatex' || \
GPVAL_TERM eq 'pngcairo' || \
(GPVAL_TERM eq 'postscript' && strstrt(GPVAL_TERMOPTIONS, 'eps ') == 1)) {
TSCALE = 20.0
} else {
if (GPVAL_TERM eq 'svg' || GPVAL_TERM eq 'postscript') {
TSCALE = 10.0
} else {
TSCALE = 1.0
}
}
从gnuplot 5.0开始,一个单独的变量GPVAL_TERM_SCALE
可用,该变量已经包含所选终端的适当值,并且使此复杂检查过时了.
Since gnuplot 5.0 a separate variable GPVAL_TERM_SCALE
is available which already contains the appropriate value for the selected terminal and make this complex check obsolete.
有很多方法可以自动执行此操作.您可以例如将这些定义写成字符串,并在需要时在它们上调用eval
:
There are many ways to automate this. You can e.g. write these definitions in strings and just call eval
on them when you need them:
save_margins = 'TSCALE = 1.0;'\
'LMARGIN = TSCALE*GPVAL_TERM_XMIN/(1.0*GPVAL_TERM_XSIZE);'.\
'RMARGIN = TSCALE*GPVAL_TERM_XMAX/(1.0*GPVAL_TERM_XSIZE);'.\
'TMARGIN = TSCALE*GPVAL_TERM_YMAX/(1.0*GPVAL_TERM_YSIZE);'.\
'BMARGIN = TSCALE*GPVAL_TERM_YMIN/(1.0*GPVAL_TERM_YSIZE);'
plot sin(x)
eval(save_margins)
print LMARGIN
并相应地
restore_margins = 'set lmargin at screen LMARGIN;'.\
'set rmargin at screen RMARGIN;'.\
'set tmargin at screen TMARGIN;'.\
'set bmargin at screen BMARGIN'
eval(restore_margins)
通用用法
要尽可能通用地使用这些功能,只需将以下内容放在脚本中,例如fixed-margins.gp
:
save_margins = 'LMARGIN = TSCALE*GPVAL_TERM_XMIN/(1.0*GPVAL_TERM_XSIZE);'.\
'RMARGIN = TSCALE*GPVAL_TERM_XMAX/(1.0*GPVAL_TERM_XSIZE);'.\
'TMARGIN = TSCALE*GPVAL_TERM_YMAX/(1.0*GPVAL_TERM_YSIZE);'.\
'BMARGIN = TSCALE*GPVAL_TERM_YMIN/(1.0*GPVAL_TERM_YSIZE);'
restore_margins = 'set lmargin at screen LMARGIN;'.\
'set rmargin at screen RMARGIN;'.\
'set tmargin at screen TMARGIN;'.\
'set bmargin at screen BMARGIN;'
set_fixed_margins = save_margins . restore_margins
if (exists('GPVAL_TERM_SCALE')) {
TSCALE = GPVAL_TERM_SCALE
} else {
if (GPVAL_TERM eq 'pdfcairo' || \
GPVAL_TERM eq 'cairolatex' || \
GPVAL_TERM eq 'pngcairo' || \
(GPVAL_TERM eq 'postscript' && strstrt(GPVAL_TERMOPTIONS, 'eps ') == 1)) {
TSCALE = 20.0
} else {
if (GPVAL_TERM eq 'svg' || GPVAL_TERM eq 'postscript') {
TSCALE = 10.0
} else {
TSCALE = 1.0
}
}
}
要使用此功能,只需在设置端子后load
脚本,然后在绘图中适当位置的eval
边距字符串:
To use this feature, just load
the script after having set the terminal, and then eval
the margin string at the appropriate positions in the plot:
set terminal ...
load 'fixed-margins.gp'
set multiplot
plot sin(x)
eval(set_fixed_margins)
....
这篇关于Gnuplot:存储绘图区域尺寸以备后用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!