gnuplot stacked filledcurves can't show the corrects sum

独自空忆成欢 提交于 2019-12-01 21:11:59

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 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!