【问题标题】:GNUPLOT - Two columns histogram with values on top of barsGNUPLOT - 两列直方图,条形顶部有值
【发布时间】:2017-03-21 22:36:16
【问题描述】:

昨天我提出了一个类似的问题 (this one)。我无法在 gnuplot 直方图中显示条形顶部的值。我浪费了很多时间,因为我找不到关于它的真正好的文档,而且我只能在不同的网站上找到类似的问题。

我为此浪费了很多时间,但幸运的是有人给了我解决方案。现在,我在带有两个条形图的直方图上遇到了类似的问题,我必须将其值放在两个条形图的顶部。我已经很近了,或者这就是我的想法,但我无法使其正常工作。我多次更改脚本并重新生成图表,但我不确定自己在做什么。

script.sh

#!/usr/bin/gnuplot
set term postscript
set terminal pngcairo nocrop enhanced size 600,400 font "Siemens Sans,8"
set termoption dash
set output salida
set boxwidth 0.8 absolute
set border 1
set style fill solid 1.00 border lt -1
set key off
set style histogram clustered gap 1 title textcolor lt -1
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror autojustify
set xtics  norangelimit
set xtics ()
unset ytics
set title titulo font 'Siemens Sans-Bold,20'
set yrange [0.0000 : limite1] noreverse nowriteback
set y2range [0.0000 : limite2] noreverse nowriteback
show style line


set style line 1 lt 1 lc rgb color1 lw 1
set style line 2 lt 1 lc rgb color2 lw 1

## Last datafile plotted: "immigration.dat"
plot fuente using 2:xtic(1) ls 1 ti col axis x1y1, '' u 3 ls 2 ti col axis x1y2, '' u 0:2:2 with labels offset -3,1 , '' u 0:2:3 with labels offset 3,1

我正在修改最后一行代码,因为这里是我设置标签的地方。我已经能够显示两个标签,但是在不好的位置上,我也能够在正确的位置显示一个标签,但不能显示另一个。除了我想要的东西,我几乎可以展示所有东西。这是生成脚本的图表。

输出.png

这是我用来生成图表的源文件

source.dat

"Momento" "Torre 1" "Torre 2" 
"May-16" 1500.8 787.8
"Jun-16" 1462.3 764.1
"Jul-16" 1311.2 615.4
"Ago-16" 1199.0 562.0
"Sep-16" 1480.0 713.8
"Oct-16" 1435.1 707.8

这就是我使用参数集执行的命令

gnuplot -e "titulo='Energía consumida por torre (MWh)'; salida='output.png'; fuente='source.dat'; color1='#FF420E'; color2='#3465A4'; limite1='1800.96'; limite2='945.36'" script.sh

我认为这很明显我在假装,有人可以帮助我吗?

非常感谢。

【问题讨论】:

    标签: gnuplot histogram


    【解决方案1】:

    您的脚本有几个问题,缺少的ti col 只是其中之一。 (你也可以使用set key auto columnheader,那么你不能每次都给那个选项。

    • 如果要比较值,请不要同时使用 y1y2 轴!否则,正确的条形高度只是运气问题...

    • 了解 gnuplot 如何定位直方图条,然后您可以准确定位每个条的顶部中心。如果您仅将 offsetchar 值一起使用(仅提供数字时就是这种情况),那么您的脚本将在您添加或删除数据行时立即中断。

    直方图簇从 x 位置 0 开始,定位在整数 x 值的中心。由于您在每个集群中有两个条形图并且间隔为 1,因此第一个条形图的中心位于 ($0 - 1/6.0) (= 1/(2 * (numberOfTorres + gapCount))),第二个条形图的中心位于 ($0 + 1/6.0)

    set terminal pngcairo nocrop enhanced size 600,400 font ",8"
    set output 'output.png'
    set title 'Energía consumida por torre (MWh)' font ",20"
    set boxwidth 0.8 absolute
    set border 1
    set style fill solid 1.00 border lt -1
    set style histogram clustered gap 1 title textcolor lt -1
    set style data histograms
    set xtics border scale 1,0 nomirror autojustify norangelimit
    unset ytics
    
    set key off auto columnheader
    set yrange [0:*]
    set offset 0,0,graph 0.05,0
    
    set linetype 1 lc rgb '#FF420E'
    set linetype 2 lc rgb '#3465A4'
    # dx = 1/(2 * (numberOfTorres + gap))
    dx = 1/6.0
    
    plot 'source.dat' using 2:xtic(1),\
         '' u 3,\
         '' u ($0 - dx):2:2 with labels,\
         '' u ($0 + dx):3:3 with labels
    

    现在,从条形中心开始,您可以安全地使用offset 仅指定相对于条形顶部中心的偏移量:

    plot 'source.dat' using 2:xtic(1),\
             '' u 3,\
             '' u ($0 - dx):2:2 with labels offset -1,1 ,\
             '' u ($0 + dx):3:3 with labels offset 1,1
    

    第二种选择是使用标签的对齐方式:红色条的标签在条的右边界右对齐,蓝色条的标签在条的左边界左对齐:

    absoluteBoxwidth = 0.8
    dx = 1/6.0 * (1 - absoluteBoxwidth)/2.0
    
    plot 'source.dat' using 2:xtic(1),\
             '' u 3,\
             '' u ($0 - dx):2:2 with labels right offset 0,1 ,\
             '' u ($0 + dx):3:3 with labels left offset 0,1
    

    在任何情况下,这两个选项都可以使您的脚本更健壮地抵抗输入数据的变化。

    【讨论】:

    【解决方案2】:

    这看起来更好:

    plot fuente using 3:xtic(1) ls 1 ti col axis x1y1, '' u 3 ls 2 ti col axis x1y2, '' u ($0-1):3:3 with labels offset -3,1 , '' u ($0-1):2:2 with labels offset 3,1
    

    您有 2 个绘图命令:仅显示第一个。 此外,script.sh 应该是一个 bash 脚本。这是一个 gnuplot 脚本,所以它应该有另一个扩展名。

    【讨论】:

    • 非常感谢!我给出正确答案的人是我的同事,他发现了发生了什么,我邀请他在这里回答!此外,他的图表看起来有点接近,因为标签和条形之间的边距更小!还是非常感谢你
    【解决方案3】:

    问题在于ti col 选项卡。您需要将它放在每个选项中,包括标签,而不仅仅是条形图。正确的代码是:

    plot fuente using 2:xtic(1) ls 1 ti col, '' u 3 ls 2 ti col, '' u 0:2:2 ti col with labels offset -3,1 , '' u 0:3:3 ti col with labels offset 3,1
    

    这就是现在图片的显示方式:

    您也可以避免使用ti col,这就是它的外观:

    【讨论】:

      猜你喜欢
      • 2017-06-04
      • 2014-12-07
      • 2011-07-24
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      • 2012-08-26
      • 1970-01-01
      相关资源
      最近更新 更多