【问题标题】:Gnuplot auto-size?Gnuplot 自动调整大小?
【发布时间】:2019-06-12 23:51:06
【问题描述】:

我可以用 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

是否有一个选项可以让我自动管理它,以便百分比不再重叠等等......?

我认为我们必须使用画布大小管理,但我不知道如何...你能帮帮我吗?

【问题讨论】:

  • $2+1 在 Y 轴上将 1 添加到百分比标签的位置; " %g%%" 中的空格数是标签与第一个条的距离。例如,在第一个图中,您可以使用$2+0.2 和"________%g%%",其中“_”是一个空格。要自动管理它,请使用添加作为整个规模的一部分。

标签: bash gnuplot


【解决方案1】:

除了使用直方图,您还可以使用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 ...

结果:

【讨论】:

  • 我对你的提议很感兴趣,但是当我尝试将你的 gnuplot 代码与我的 .csv 一起使用时(看起来像 2018 年 7 月,19%,69%,有很多行),我有这个error : "test1.txt:1: line 0: 非法月份" 你知道为什么会出现这个错误吗?
  • 对不起,我把数据格式改成了07.2018 47 18。现在,我将$Data2 更改为您的原始格式:July 2018, 47, 18。
  • 感谢您的帮助@theozh!如果我理解正确,第一个图用于第一个图,第二个图用于第二个图...我需要一个图,所以我删除第一个图的元素,对于第二个图,我将 plot $Data2 替换为`plot test1.txt',我的数据所在的文件。但我认为,我有一个问题:* Gnuplot 错误 => [image.noelshack.com/fichiers/2019/24/4/1560427398-aaaa.png] * Graph => [image.noelshack.com/fichiers/2019/24/4/1560427438-cluster.png] * Gnu 代码 => [pastebin.com/fKxQZH1Y]
  • 错误消息的第一个链接...找不到页面。你能查一下吗?
  • 哦,当然可以!这里:[链接]image.noelshack.com/fichiers/2019/24/5/1560520013-aaaaa.png
猜你喜欢
  • 2012-06-05
  • 2021-11-25
  • 2018-02-06
  • 2015-03-13
  • 2013-12-16
  • 2015-06-16
  • 2011-04-02
  • 2013-07-16
  • 1970-01-01
相关资源
最近更新 更多