2014-05-20 28 views
1

我想繪製一段時間內堆積的直方圖。原來這與Using gnuplot for stacked histograms不同。通過gnuplot隨時間變化的堆積直方圖

這裏的數據:

05/11/2014 10:00:00 1 5 1 
05/12/2014 22:00:00 3 5 1 
05/13/2014 13:00:00 4 4 1 
05/14/2014 09:00:00 3 4 1 
05/15/2014 04:00:00 1 2 1 

的前兩列用空格隔開,其餘由製表符分隔。 x軸應該是日期和時間。

以下gnuplot的腳本是有問題的:

set title "Test" 
set key invert reverse Left outside 
set key autotitle columnheader 
set style data histogram 
set style histogram rowstacked 
set style fill solid border -1 
set boxwidth 0.75 
set datafile separator '\t' 
set xdata time 
set timefmt '%M/%D/%Y %H:%M:%S' 
set format x '%M/%D %H' 
plot 'test.dat' using 2:xtic(1) title 'Col1', '' using 3 title 'Col2', '' using 4 title 'Col3' 

以上腳本將導致錯誤:需要使用規範完整的X時間數據。但是,如果未設置xdata,它將起作用。

回答

3

set xdata time部分是確實錯了你的情況。

直方圖工作從其他繪圖風格有點不同:這些箱子被放置在整x值012等,並得到一個自定義標籤,而你的情況是包含在第一列中的時間信息。所以x軸不是實時軸。

下面的腳本工作正常4.6.4:

set title "Test" 
set key invert reverse Left outside 
set style data histogram 
set style histogram rowstacked 
set style fill solid border -1 
set boxwidth 0.75 
set datafile separator '\t' 

plot 'test.dat' using 2:xtic(1) title 'Col1', '' using 3 title 'Col2', '' using 4 title 'Col3' 

enter image description here

如果要更改用於標籤上的時間格式,必須手工解析的時間字符串。 xtic(1)相當於xtic(strcol(1))

set title "Test" 
set key invert reverse Left outside 
set style data histogram 
set style histogram rowstacked 
set style fill solid border -1 
set boxwidth 0.75 
set datafile separator '\t' 

plot 'test.dat' using 2:xtic(strftime('%m/%d %Hh', strptime('%m/%d/%Y %H:%M:%S', strcol(1)))) title 'Col1',\ 
    '' using 3 title 'Col2', '' using 4 title 'Col3' 

enter image description here

1

如何保持簡單,只需在雙引號內使用日期和時間值。所以,你的數據文件將是:

"05/11/2014 10:00:00" 1 5 1 
"05/12/2014 22:00:00" 3 5 1 
"05/13/2014 13:00:00" 4 4 1 
"05/14/2014 09:00:00" 3 4 1 
"05/15/2014 04:00:00" 1 2 1 

和你的繪製腳本是:

set title "Test" 
set key invert reverse Left outside 
#set key autotitle columnheader 
set style data histogram 
set style histogram rowstacked 
set style fill solid border -1 
set boxwidth 0.75 
set xtics border in scale 0,0 nomirror rotate by 90 offset character 0, -9, 0 
plot 'test.dat' using 2:xtic(1) title 'Col1', '' using 3 title 'Col2', '' using 4 title 'Col3' 

和由此產生的情節是:

enter image description here

+0

如果我只是想什麼:除了使用strcol(1),其中包含在第一列串的信息,你可以爲了改變顯示的時間信息處理與strptimestrftime這個數據顯示x軸的日期+小時。例如,使用設置格式x'%M /%D%H'? –

+0

爲此,您可以相應地更改數據文件,如: '「05/12/22:00」1 5 1'而不是'「05/11/2014 10:00:00」1 5 1' –

+1

有不需要改變數據文件的所有內容;)@erictan正如我在答案中指出的那樣:對於直方圖,x軸的行爲不依賴於「set format x」。查看我的答案,以便每次更改xticlabels而不更改數據文件。 – Christoph