2010-08-07 74 views
3

我有這個文件data.datgnuplot中的數字x軸的直方圖?

Xstep Y1 Y2 Y3 Y4 
332 1.22 0.00 0.00 1.43 
336 5.95 12.03 6.11 10.41 
340 81.05 81.82 81.92 81.05 
394 11.76 6.16 10.46 5.87 
398 0.00 0.00 1.51 1.25 
1036 0.03 0.00 0.00 0.00 

我可以用這個腳本繪製這個數據直方圖,hist-v1.gplot(使用set style data histogram):

set xlabel "X values" 
set ylabel "Occurence" 
set style data histogram 
set style histogram cluster gap 1 
set style fill solid border -1 
set term png 
set output 'hist-v1.png' 
set boxwidth 0.9 
# attempt to set xtics so they are positioned numerically on x axis: 
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036) 
# ti col reads the first entry of the column, uses it as title name 
plot 'data.dat' using 2:xtic(1) ti col, '' u 3 ti col, '' u 4 ti col, '' u 5 ti col 

,並通過調用:

gnuplot hist-v1.gplot && eog hist-v1.png 

生成此圖像: image hist-v1.png http://img202.imageshack.us/img202/3974/histv1.png

但是,您可以注意到X軸沒有數字縮放 - 它將X值理解爲類別(即,它是一個類別軸)。

我可以,hist-v2.gplot(使用with boxes)得到具有下列腳本多個數值X軸:

set xlabel "X values" 
set ylabel "Occurence" 
# in this case, histogram commands have no effect 
set style data histogram 
set style histogram cluster gap 1 
set style fill solid border -1 
set term png 
set output 'hist-v2.png' 
set boxwidth 0.9 
set xr [330:400] 
# here, setting xtics makes them positioned numerically on x axis: 
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036) 
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"! 
plot 'data.dat' using 1:2 ti col smooth frequency with boxes, '' u 1:3 ti col smooth frequency with boxes 

,並通過調用:產生

gnuplot hist-v2.gplot && eog hist-v2.png 

此圖像: image hist-v2.png http://img266.imageshack.us/img266/6717/histv2.png

不幸的是,這裏的酒吧'重疊',所以很難閱讀圖表。

有沒有辦法讓數字刻度X軸爲hist-v2.png,但在hist-v1.png保持並排的「酒吧」的一面?此線程,「Re: Histogram with x axis date error」說,你不能:

但是這將是很難拉x座標最新出來的數據文件,...

但後來,它指的是一個不同的問題...

謝謝,

乾杯!

回答

2

好的,在閱讀了gnuplot幫助之後,似乎直方圖風格將「總是」將x軸解釋爲順序入口/類別 - 確實,似乎沒有辦法獲得數值軸直方圖風格。

但是,事實證明$可以引用一列,並且這些可以用於在第二個(frequency with boxes樣式)示例中實際「重新定位」條塊;所以,此代碼爲hist-v2b.gplot

set xlabel "X values" 
set ylabel "Occurence" 
set style fill solid border -1 
set term png 
set output 'hist-v2.png' 
set boxwidth 0.9 
set xr [330:400] 
# here, setting xtics makes them positioned numerically on x axis: 
set xtics ("332" 332, "336" 336, "340" 340, "394" 394, "398" 398, "1036" 1036) 
# 1:2 will ONLY work with proper xr; since we have x>300; xr[0:10] generates "points y value undefined"! 
plot 'data.dat' using ($1-0.5):2 ti col smooth frequency with boxes, '' u ($1-0.25):3 ti col smooth frequency with boxes, '' u ($1+0.25):4 ti col smooth frequency with boxes, '' u ($1+0.5):5 ti col smooth frequency with boxes 

,並通過調用:產生

gnuplot hist-v2b.gplot && eog hist-v2b.png 

這一形象: image hist-v2b.png http://img823.imageshack.us/img823/805/histv2b.png

...這是一個很值得我首先想。

只是一個小紙條 - 我原本想用內嵌數據的腳本;像這樣的設置,就必須寫成

plot '-' using ($1-0.5):2 ti col smooth frequency with boxes, '-' u ($1-0.25):3 ti col smooth frequency with boxes 
Xstep Y1 Y2 Y3 Y4 
332 1.22 0.00 0.00 1.43 
336 5.95 12.03 6.11 10.41 
340 81.05 81.82 81.92 81.05 
394 11.76 6.16 10.46 5.87 
398 0.00 0.00 1.51 1.25 
1036 0.03 0.00 0.00 0.00 
end 
Xstep Y1 Y2 Y3 Y4 
332 1.22 0.00 0.00 1.43 
336 5.95 12.03 6.11 10.41 
340 81.05 81.82 81.92 81.05 
394 11.76 6.16 10.46 5.87 
398 0.00 0.00 1.51 1.25 
1036 0.03 0.00 0.00 0.00 
end 

...也就是說,該數據將不得不多次輸入,因爲它從標準輸入進來 - 這個問題在gnuplot - do multiple plots from data file with built-in commands討論。

乾杯! PS:由於圖表上有相當多的空間,如果我們能以某種方式指定單獨的x軸範圍,那將是很好的;這是在討論:

1

設置框的寬度,當你使用繪製「盒子」打印樣式直方圖正確是非常重要的。在我的一篇博客文章中,我已經討論過它。如果有興趣,請點擊here

enter image description here

+0

非常感謝您發佈您的解決方案,@ hsxz - 乾杯! – sdaau 2011-09-19 19:30:03