Eric
Eric

Reputation: 351

Stacked histogram over time via gnuplot

I'd like to plot a stacked histogram over time. This turned out to be different from the Using gnuplot for stacked histograms.

Here's the data:

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

The first two columns are separated by spaces and the rest are separated by tabs. The x-axis should be the date and time.

The following gnuplot script is problematic:

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'

The above script will result in an error: Need full using spec for x time data. However, it works if unset xdata.

Upvotes: 3

Views: 2502

Answers (2)

Christoph
Christoph

Reputation: 48390

The set xdata time part is indeed wrong in your case.

Histograms work a bit different from other plotting styles: The boxes are placed at integer x-values 0, 1, 2 etc. and get a custom label, which in your case is the time information contained in the first column. So the x-axis isn't a real time axis.

The following script works fine with 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

If you want to change the time format used for the labels, you must parse the time string manually. xtic(1) is equivalent to xtic(strcol(1)). Instead of using strcol(1), which contains the information in the first column as string, you can process this data with strptime and strftime in order to change the displayed time information:

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

Upvotes: 7

Zahaib Akhtar
Zahaib Akhtar

Reputation: 1078

How about keeping it simple and just using the date and time values inside double quotes. So your data file would be:

"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

And your plotting script would be:

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'

And resultant plot would be:

enter image description here

Upvotes: 1

Related Questions