2015-06-16 30 views
3

這個問題與gnuplot histogram: How to put values on top of bars有關。gnuplot rowstacked直方圖:如何把總和以上的酒吧

我有一個數據文件file.dat

x y1 y2 
1 2 3 
2 3 4 
3 4 5 

和gnuplot的:

set style data histogram; 
set style histogram rowstacked; 
plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col; 

現在我要放置金額2列和3個以上吧。明顯的解決方案

plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col, \ 
'' u ($0-1):($2+$3+0.2):($2+$3) notitle w labels font "Arial,8"; 

將標籤放在正確的位置,但計算的總和是錯誤的。也就是說,在($0-1):($2+$3+0.2):($2+$3)中,第二個$2似乎評估爲零。

這裏怎麼回事,我該如何解決?

回答

2

你必須給一個明確的字符串作爲標籤:

plot newhistogram 'foo', 'file.dat' u 2:xtic(1) t col, '' u 3 t col, \ 
'' u ($0-1):($2+$3):(sprintf('%.1f', $2+$3)) notitle w labels offset 0,1 font "Arial,8" 

至於其他改善,我會使用offset選項,允許你給在字符爲單位的位移,它不依賴於yrange。

(附註:如果使用從列中的值,那麼就可以跳過標籤的明確的格式,如using 1:2:2 with labels,但一般應使用sprintf格式化標籤)

+0

注意的是,在如果數據文件中有正數和負數,正確的偏移量不再是常量,因此不幸的是,您不能使用'偏移量'(它需要一個固定位移),但必須使用依賴於yrange的方法。 –

+0

是的,你是對的。有一個非常非常醜陋的黑客:當值爲負數時插入一些新行:'sprintf(「%s%.1f」,($ 2 + $ 3)<0?「\ n \ n」:「」,$ 2 + $ 3)'。免責聲明:不要在家中試用;) – Christoph