GNUPLOT-两列直方图,条形图顶部有值 [英] GNUPLOT - Two columns histogram with values on top of bars

查看:218
本文介绍了GNUPLOT-两列直方图,条形图顶部有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

yesteraday I made a similar question (this one). I could not display the value on top of bar in a gnuplot histogram. I lost many time because I couldn't find really good documentation about it, and I only can find similar issues on differents websites.

我为此花了很多时间,但幸运的是有人给了我解决方案.现在,我对带有两个条形图的直方图也遇到了类似的问题,其中我必须将其值放在两个条形图的顶部.我离我很近,或者那是我的想法,但是我无法使其正常运行.我正在多次更改脚本并重新生成图形,但是我不确定自己在做什么.

I lost many time with that but fortunately someone give me the solution. Now I am having a similar issue with an histogram with two bars, in which I have to put on top of both bars its value. I am quite near, or that is what I think, but I can't make it work properly. I am changing the script and regenerating the graph many times but I am not sure of what I am doing.

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

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

I am modifying the last code line, because is here where I set the labels. I have been able to show both labels, but in bad positions, I have also been able to show one of the labels in the right position but no the other. I have been able to show almost everything but the thing that I want. This is the graph that generates the script.

output.png

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

This is the source file that I use for generating the graph

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

这就是我在设置了参数的情况下执行的命令

And that's the command that I execute with the parameters set

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

我认为我假装的内容很明显,有人可以帮助我吗?

I think that is quite obvious what I am pretending, can someone help me?

预先表示感谢.

推荐答案

您的脚本有几个问题,缺少的ti col只是其中之一. (您也可以使用set key auto columnheader,这样就不必每次都给出该选项).

Your script has several problems, the missing ti col is only one of them. (You can also use set key auto columnheader, then you must not give that option every time).

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

  • Don't use both y1 and y2 axis if you want to compare the values! Otherwise the correct bar heights are only a matter of luck...

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

Understand, how gnuplot positions the histogram bars, then you can exactly locate the top center of each bar. If you only use offset with char values (which is the case when you give only numbers), then your script will break as soon as you add or remove a data row.

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

The histogram clusters start at x-position 0, and are positioned centered at integer x values. Since you have two bars in each cluster and a gap of 1, the center of the first bar is at ($0 - 1/6.0) (= 1/(2 * (numberOfTorres + gapCount))), the second one at ($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仅指定相对于钢筋顶部中心的偏移量:

Now, starting at the bars center you can safely use offset to specify only the offset relative to the bars top center:

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

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

A second option would be to use the label's alignment: The labels of the red bars are right aligned at the bars right border, the labels of the blue bars are left aligned at the bars left border:

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

无论如何,这两个选项都使您的脚本对输入数据的更改更加健壮.

In any case, both options make your script more robust against changes of the input data.

这篇关于GNUPLOT-两列直方图,条形图顶部有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆