Currently I working on Gnulot stacked filledcurves. I have problem to make my graphs stacked.
This is my data:
prog reli perf avail sec cons topo scale qos
2011 138 90.3 21.0 63.5 45.5 48.5 6.8 4.0 5.5
2012 191.3 77.8 90.8 30.8 29.0 22.1 2.0 1.0 1.0
2013 85.0 57.5 48.0 20.0 27.5 8.5 0 2.5 1.0
2014 2.0 0.5 1.0 2.0 1.0 1.5 0 0 0
I have plotted on t1.plt
set term pos eps font 20
set output 't1.eps'
set pointsize 0.8
set border 11
set xtics out
set tics front
set key below
set multiplot
a=0
plot for [i=1:9] "t1" using (column(i)):xtic(1) t column(i) with filledcurves
my current output:
my expectation to create graphs like this link:
Here is how you can do this with gnuplot only. You can use the sum
command for the summation of the column values in order to get a stacked graph:
set terminal postscript eps color font 20
set output 't1.eps'
set xtics 1 out
set tics front
set key invert
set style fill solid noborder
plot for [i=10:2:-1] "t1" using 1:(sum [col=2:i] column(col)) with filledcurves x1 title columnheader(i-1)
Note, that the indices for the column headers are 1..9, whereas the values go from 2..10. So you must explicitely use title columnheader(i-1)
. If you would give the first column also a header, e.g. year
, you could use set key autotitle columnheader
.
Unfortunately, the invert
option of set key
works only on the columns. So if you use set key below invert
, you don't get the original order of your data file.
Result with 4.6.4:
Notes on style:
Add x1
at the end of your plot
command so the curves will be closed towards the (lower) x axis (x2
would be the upper one). Note also that set multiplot
is unnecessary in this case. Finally, the title in your iteration should come from column(i-1)
rather than column(i)
, or else add a label to the first column in your data file, and the iteration should run from 2 to 10, unless you want to plot the first column against itself too.
With your data and the following command:
plot for [i=2:10] \
"t1" using (column(i)):xtic(1) t column(i-1) with filledcurves x1
I get:
Getting the graphs stacked is a bit more complicated as it involves adding up consecutive columns, it can be done with awk, invoked within gnuplot:
set xtics 1
plot for [i=10:2:-1] \
"< awk 'NR==1 {print \"year\",$".(i-1)."} NR>=2 {for (i=2; i<=".i."; i++) \
{sum+= $i} {print $1, sum; sum=0} }' t1" \
using (column(2)):xtic(1) with filledcurves x1 t column(2)
You can execute the awk
part alone outside gnuplot to see what it does to your data:
# This is for second column (col=2), change value of col variable to see other columns
awk 'col=2 {} NR==1 {print "year",$(col-1)} NR>=2 {for (i=2; i<=col; i++) {sum+= $i} {print $1, sum; sum=0} }' t1