昨天我提出了一个类似的问题(这个)。我无法在 gnuplot 直方图中显示条形顶部的值。我失去了很多时间,因为我找不到关于它的真正好的文档,而且我只能在不同的网站上找到类似的问题。
我失去了很多时间,但幸运的是有人给了我解决方案。现在,我在带有两个条形的直方图上遇到了类似的问题,其中我必须将其值放在两个条形之上。我很接近,或者这就是我的想法,但我无法使其正常工作。我多次更改脚本并重新生成图表,但我不确定我在做什么。
脚本文件
#!/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
Run Code Online (Sandbox Code Playgroud)
我正在修改最后一行代码,因为我在这里设置标签。我已经能够显示两个标签,但是在错误的位置,我也能够在正确的位置显示一个标签,但没有显示另一个。我已经能够展示几乎所有东西,但我想要的东西。这是生成脚本的图形。
输出.png
这是我用于生成图形的源文件
源文件
"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
Run Code Online (Sandbox Code Playgroud)
这就是我使用参数集执行的命令
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
Run Code Online (Sandbox Code Playgroud)
我认为这很明显我在假装,有人可以帮助我吗?
非常感谢提前。
您的脚本有几个问题,缺少的ti col只是其中之一。(你也可以使用set key auto columnheader,那么你不能每次都给出这个选项)。
如果您想比较值,请不要同时使用y1和axis!y2否则正确的酒吧高度只是运气问题......
了解 gnuplot 如何定位直方图条形,然后您就可以准确定位每个条形的顶部中心。如果您仅使用offset值char(当您仅给出数字时就是这种情况),那么一旦您添加或删除数据行,您的脚本就会中断。
直方图簇从 x-position 开始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"\nset output \'output.png\'\nset title \'Energ\xc3\xada consumida por torre (MWh)\' font ",20"\nset boxwidth 0.8 absolute\nset border 1\nset style fill solid 1.00 border lt -1\nset style histogram clustered gap 1 title textcolor lt -1\nset style data histograms\nset xtics border scale 1,0 nomirror autojustify norangelimit\nunset ytics\n\nset key off auto columnheader\nset yrange [0:*]\nset offset 0,0,graph 0.05,0\n\nset linetype 1 lc rgb \'#FF420E\'\nset linetype 2 lc rgb \'#3465A4\'\n# dx = 1/(2 * (numberOfTorres + gap))\ndx = 1/6.0\n\nplot \'source.dat\' using 2:xtic(1),\\\n \'\' u 3,\\\n \'\' u ($0 - dx):2:2 with labels,\\\n \'\' u ($0 + dx):3:3 with labels\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n现在,从条形中心开始,您可以安全地使用offset仅指定相对于条形顶部中心的偏移量:
plot \'source.dat\' using 2:xtic(1),\\\n \'\' u 3,\\\n \'\' u ($0 - dx):2:2 with labels offset -1,1 ,\\\n \'\' u ($0 + dx):3:3 with labels offset 1,1\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n第二个选项是使用标签的对齐方式:红色条的标签在条的右边框处右对齐,蓝色条的标签在条的左边框处左对齐:
\n\nabsoluteBoxwidth = 0.8\ndx = 1/6.0 * (1 - absoluteBoxwidth)/2.0\n\nplot \'source.dat\' using 2:xtic(1),\\\n \'\' u 3,\\\n \'\' u ($0 - dx):2:2 with labels right offset 0,1 ,\\\n \'\' u ($0 + dx):3:3 with labels left offset 0,1\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n无论如何,这两个选项都可以使您的脚本对输入数据的更改更加稳健。
\n