3

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:

output

my expectation to create graphs like this link: expectation output

indi60
  • 867
  • 4
  • 17
  • 34

2 Answers2

5

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:

enter image description here

Christoph
  • 47,569
  • 8
  • 87
  • 187
  • This is obviously the correct answer. Since what version is `sum` implemented? – Miguel May 26 '14 at 19:36
  • @Miguel This works since 4.6.0. The only problem could be the order of the single key labels, since `invert` doesn't invert it completely. – Christoph May 26 '14 at 19:45
  • @Christoph: you always be a hero. Thanks! – indi60 May 27 '14 at 00:13
  • @Christoph, little thing about legend orders. The orders should be perf, avail, sec, and so on. and also I can see on top of curves any orange color. Isn't true? – indi60 May 27 '14 at 00:38
  • @indi60 See the edit. There was also a column missing in my previous version. If you also want to reverse the line types, you must add an `lt i-1` – Christoph May 27 '14 at 07:29
3

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:

enter image description here

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)

enter image description here

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 
Miguel
  • 7,497
  • 2
  • 27
  • 46
  • Thanks @miguel.. btw are this graphs stacked? I guess every category start from zero. I expect the next category start from the end of previous category. i.e: prog 0-20 and the reli should start from 20-30 and so on. – indi60 May 26 '14 at 10:45