3

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.

Community
  • 1
  • 1
Eric
  • 341
  • 1
  • 2
  • 7

2 Answers2

7

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

Christoph
  • 47,569
  • 8
  • 87
  • 187
1

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

Zahaib Akhtar
  • 1,068
  • 2
  • 11
  • 26
  • What if I just want to show date + hour for the x-axis. For example, use set format x '%M/%D %H'? – Eric May 20 '14 at 21:39
  • For that you can change your data file accordingly, like: `"05/12/22:00" 1 5 1` instead of `"05/11/2014 10:00:00" 1 5 1` – Zahaib Akhtar May 20 '14 at 21:51
  • 1
    There is no need to change the data file for everything ;) @erictan As I pointed out in my answer: For histograms the behaviour of the x-axis doesn't depend on `set format x`. See my answer for a way to change the xticlabels without changing the data file every time. – Christoph May 20 '14 at 23:21
  • Thats great, general question about Gnuplot @Christoph: Does Gnuplot inherit all C functions? That is, can we possibly use any C function while writing Gnuplot code? – Zahaib Akhtar May 21 '14 at 01:02
  • No, there is a very limited number of string functions available in gnuplot. Have a look at the documentation in the section `Expressions -> Functions`: `gprintf`, `sprintf`, `strlen`, `strstrt`, `substr`, `strftime`, `strptime`. – Christoph May 21 '14 at 07:13