To plot stacked histogram in Gnuplot from this data

Data

Year    Water Power     Wood Power
2025    58.1     41.9
2035    55.8     44.2
2050    50.6     49.4

Code

set style histogram columnstacked; 
set terminal qt size 560,270; 
set grid; 
set offset 1,1,0,0; 
plot 'RES.dat' u 1:3;

which gives

enter image description here

where it somehow omits the first sentence of the command. I tried to set the key unsuccessfully by plot 'RES.dat' u 3:key(1); which I think should not be necessary since, in 1:3, the key is already assumed to be set by the first column.

Another try with code

set terminal qt size 560,270; 
set grid; set offset 1,1,0,0; 
plot 'RES.dat' u 2 ti col, u 3:key(1) ti col;

but wrong.

I want something like

enter image description here

where stacked columns as described in the manual but for total 100 units. My implementation seems to be wrong. Possible solution here.

How can you get a stacked histogram in Gnulplot?

Upvotes: 2

Views: 744

Answers (1)

Christoph
Christoph

Reputation: 48390

Several things:

  • You must tell gnuplot to use histograms with e.g.

    set style data histogram
    
  • When plotting histograms you must not specify the x-value. This is choosen automatically (increasing integer, starting at 0)

  • To use a column title with spaces, you must surround it with quotes.

Here is a full, working example to get a column stacked histogram:

$data <<EOD
Year    "Water Power"     "Wood Power"
2025    58.1     41.9
2035    55.8     44.2
2050    50.6     49.4
EOD

set style histogram columnstacked
set style data histogram 
set key autotitle columnheader
set style fill solid noborder
set boxwidth 0.8
plot $data using 2, '' using 3:key(1)

enter image description here

In order to get a row stacked histogram, you only need very minor changes (use xtic instead of key):

$data <<EOD
Year    "Water Power"     "Wood Power"
2025    58.1     41.9
2035    55.8     44.2
2050    50.6     49.4
EOD

set style histogram rowstacked
set style data histogram 
set key autotitle columnheader
set style fill solid noborder
set boxwidth 0.8
set offset 0,1,0,0
plot $data using 2:xtic(1), '' using 3

enter image description here

Upvotes: 2

Related Questions