Gnuplot自动调整大小?

时间:2019-05-03 13:55:47

标签: bash gnuplot

我可以使用gnuplot创建图形。我用这个模板来做:

f(w) = (strlen(w) > 10 ? word(w, 1) . "\n" . word(w, 2) : w)

set title "TITLE"
set terminal png truecolor size 960, 720 background rgb "#eff1f0"
set output "/var/www/html/CLUSTER_NAME.png"
set bmargin at screen 0.1
set key center top
set grid
set style data histograms
set style fill solid 1.00 border -1
set boxwidth 0.7 relative
set yrange [*:*]
set format y "%g%%"
set datafile separator ","
plot 'test1.txt' using 2:xtic(f(stringcolumn(1))) title " CPU consumption (%) ", \
'' using 3 title " RAM consumption (%)", \
'' using 0:($2+1):(sprintf("%g%%",$2)) with labels notitle, \
'' using 0:($3+1):(sprintf("     %g%%",$3)) with labels notitle

TITLE和CLUSTER_NAME只是允许我通过另一个脚本进行sed以便用正确的名称替换这些名称的名称。

开始时,我是用它来管理图形的大小:

set yrange [0:]

但是现在,我使用它来自动管理大小:

set yrange[*:*]

问题是:对于某些图形,某些百分比重叠,或者百分比超出框架,或者条与百分比之间的间距太大...

http://image.noelshack.com/fichiers/2019/18/5/1556891635-cluster-cpy-01.png http://image.noelshack.com/fichiers/2019/18/5/1556891641-cluster-mo1-erp-bb-01.png

没有选项可以让我自动进行管理,以使百分比不再重叠等等吗??

我认为我们必须处理画布尺寸管理,但是我不知道如何...您能帮我吗?

1 个答案:

答案 0 :(得分:0)

除了使用直方图,您还可以使用with boxes并根据需要在x方向上移动它们。这样,我想将标签自动设置在“很好”的位置会更容易。偏移量以字符为单位。我猜您必须手动set yrange,否则在图形的边界上将具有最小值和最大值。

代码:

### histogram like with boxes and labels
reset session

$Data1 <<EOD
07.2018 12 9
08.2018 14 9
09.2018 13 10
10.2018 16 10
11.2018 16 10
12.2018 16 10
01.2019 15 10
02.2019 15 10
EOD

$Data2 <<EOD
July 2018, 47, 18
August 2018, 70, 19
September 2018, 36, 21
October 2018, 20, 21
November 2018, 44, 21
December 2018, 36, 21
January 2019, 30, 21
February 2019, 31, 22
EOD

set bmargin 3
set boxwidth 0.3 relative
set xdata time
set yrange [0:20]
set format y "%g%%"
Month(s) = strftime("%B\n%Y",strptime("%m.%Y",s))
set xtics offset first 0.2

plot \
    $Data1 u ($0-0.2):2:xtic(Month(strcol(1))) w boxes fc rgb "red" fs solid 1.0 t "CPU consumption (%)", \
     '' u ($0+0.2):3 w boxes fc rgb "green" fs solid 1.0 t "RAM consumption (%)", \
     '' u ($0-0.2):2:(sprintf("%g%%",$2)) w labels center offset 0,0.7 notitle, \
     '' u ($0+0.2):3:(sprintf("%g%%",$3)) w labels center offset 0,0.7 notitle

pause -1

set datafile separator ","
Month(n) = word(strcol(n),1)."\n".word(strcol(n),2)
set yrange[0:75]
plot \
    $Data2 u ($0-0.2):2:xtic(Month(1)) w boxes fc rgb "red" fs solid 1.0 t "CPU consumption (%)", \
     '' u ($0+0.2):3 w boxes fc rgb "green" fs solid 1.0 t "RAM consumption (%)", \
     '' u ($0-0.2):2:(sprintf("%g%%",$2)) w labels center offset 0,0.7 notitle, \
     '' u ($0+0.2):3:(sprintf("%g%%",$3)) w labels center offset 0,0.7 notitle
### end of code

编辑:

我将$Data2的格式更改为您的原始输入格式July 2018, 47, 18。 要处理这种格式,您需要在第二个绘图之前(已插入上方)添加以下行。

set datafile separator ","
Month(n) = word(strcol(n),1)."\n".word(strcol(n),2)

并更改行:

plot \
    $Data2 u ($0-0.2):2:xtic(Month(1)) w boxes ...

结果:

enter image description here

enter image description here