On Tue, 5 Dec 2017, Artur Tarassow wrote:
Dear all,
I know this is not the gnuplot forum but I hope that somebody knows how to
solve this using gretl + gnuplot (v. 5 patchlevel 5).
I want to plot a stacked barplot over time by providing the matrix 'mat'
(rows=Time, cols=variables) and a vector 'xmat'. xmat is based on the
$obsdate command holding daily dates in the format YYYYMMDD. My objective is
to handle date strings on the x-axis correctly. But this drives me nuts...
As you will see, for the moment I deactivated everything related to time and
its formatting. I also took Jack's function for plotting the historical
decomposition based on the SVAR package as a role model but somehow this
doesn't work for me.
I attached the function plus an example which results in the error:
<gnuplot>
plot '-' using 1:($2), '-' using 1:($2)
^
"/home/at/.gretl/gpttmp.DjR2By", line 25: Too many columns in using
specification
You're only allowed one data column in a gnuplot histogram plot
clause.
I'm sure that more elegant variants are possible, but here's an
example that does roughly what you want (I think):
<hansl>
set verbose off
function void stackplot (matrix mat,
matrix xmat[null] "add if you want tp plot vs. xmat-values",
int nstep[1::4] "Print only n-th value on xaxis",
string fname)
tmpfile = sprintf("%s/stackedbar.gp", $dotdir)
outfile @tmpfile --write
printf "set encoding utf8\n"
printf "set nokey\n"
printf "set style data histogram \n"
printf "set style histogram rowstacked\n"
printf "set style fill solid border -1\n"
printf "set style fill solid 0.35\n"
printf "set xtics border in scale 0,0 nomirror rotate by -45 autojustify\n"
# write data block
printf "$data <<EOF\n"
loop i=1..rows(mat) -q
loop j=1..cols(mat) -q
printf "%g ", mat[i,j]
endloop
if exists(xmat)
if i % nstep == 0
printf "%g", xmat[i]
else
printf "-1"
endif
endif
printf "\n"
endloop
printf "EOF\n"
# write plot specification
printf "plot \\\n"
loop j=1..cols(mat) -q
if j == 1 && exists(xmat)
k = cols(mat) + 1
printf "$data using 1:xtic($%d < 0. ? \"\" :
stringcolumn(%d))", k, k
else
printf "$data using %d", j
endif
printf(j == cols(mat) ? "\n" : ", \\\n")
endloop
outfile --close
gnuplot --input="@tmpfile" --output=@fname
end function
open data9-7 -q
smpl 1975:1 1977:4
series date = (int($obsdate/100))/100
series QNC2 = QNC * 0.5
matrix M = {QNC}~{QNC2}
stackplot(M, {date}, 2, "display")
</hansl>
Allin