3

I am starting to use pgfplots to plot time series and was looking into this example (page 365 of the documentation): enter image description here

The odd thing is that the data from the 3rd series is more or less constant, but the corresponding curve is not flat at all (brown curve).

How can that be ? I also noticed that the y values for the 2nd and 3rd points from the left on the curve are the same, but do not appear at the same level on the graph.

I tried to graph the 2nd series ("account 2") on a separate y axis, and then I get those points at the same level.

Anyway, this will prove to be a real problem for me if curves are distorted.

Any idea where this could be coming from?

Thanks in advance, Kind regards,

Nicolas

  • 1
    I think this is is just an error in the documentation. curves do not distort. This has likely happened just becouse they created the image with one set of data, and printed the table with another set. – Runar Aug 9 '16 at 18:47
2

The problem is in stack plots=y command. If you remove that option, profiles will look like time series data. Here is the minimum working example:

\documentclass[a4paper,10pt]{article}

\usepackage{pgfplots}

\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment[{[]}]{tikzpicture}

\pgfplotsset{width=7cm, compat=1.13}

\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}

\usepackage{eurosym}

\begin{document}

\pgfplotstabletypeset[string type]{plotdata/accounts.dat}

\begin{tikzpicture}
    \begin{axis}[
        date coordinates in=x,
        xticklabel={\day.\month.},
        xlabel={2008},
        %stack plots=y,
        yticklabel={\pgfmathprintnumber{\tick}\EUR{}},
        ylabel=Total credit,
        ylabel style={yshift=10pt},
        legend style={
        at={(0.5,-0.3)},anchor=north,legend columns=-1}
    ]
        \addplot table[x=date,y=account1] {plotdata/accounts.dat};
        \addplot table[x=date,y=account2] {plotdata/accounts.dat};
        \addplot table[x=date,y=account3] {plotdata/accounts.dat};
        \legend{Giro,Tagesgeld,Sparbuch}
    \end{axis}
\end{tikzpicture}

\end{document}

Just copy this code in a .tex file, and create accounts.dat file (put that file in a plotdata subfolder) with the following contents:

date            account1    account2    account3
2008-01-03      60          1200        400
2008-02-06      120         1600        410
2008-03-15      -10         1600        410
2008-04-01      1800        500         410
2008-05-20      2300        500         410
2008-06-15      800         1920        410

Build your .tex file, and you should get something like this:

pgfplots

2

It's not wrong but this is a stack plot so each account is shown in a cumulative way. You can see the option stack plot=y in the example in the next page.

Here are the source files to play with that can be found on CTAN (also in the comments)

\documentclass{article}
% translate with >> pdflatex -shell-escape <file>

% This file is an extract of the PGFPLOTS manual, copyright by Christian Feuersaenger.
% 
% Feel free to use it as long as you cite the pgfplots manual properly.
%
% See
%   http://pgfplots.sourceforge.net/pgfplots.pdf
% for the complete manual.
%
% Any required input files (for <plot table> or <plot file> or the table package) can be downloaded
% at
% http://www.ctan.org/tex-archive/graphics/pgf/contrib/pgfplots/doc/latex/
% and
% http://www.ctan.org/tex-archive/graphics/pgf/contrib/pgfplots/doc/latex/plotdata/

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pagestyle{empty}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}\usepackage{eurosym}

\begin{document}
% requires \usepgfplotslibrary{dateplot} !

\pgfplotstabletypeset[string type]{accounts.dat}

\begin{tikzpicture}
    \begin{axis}[
        date coordinates in=x,
        xticklabel={\day.\month.},
        xlabel={2008},
        stack plots=y,
        yticklabel={\pgfmathprintnumber{\tick}\EUR{}}, % <- requires \usepackage{eurosym}
        ylabel=Total credit,
        ylabel style={yshift=10pt},
        legend style={
            at={(0.5,-0.3)},anchor=north,legend columns=-1}]

    \addplot table[x=date,y=account1] {accounts.dat};
    \addplot table[x=date,y=account2] {accounts.dat};
    \addplot table[x=date,y=account3] {accounts.dat};
    \legend{Giro,Tagesgeld,Sparbuch}
    \end{axis}
\end{tikzpicture}
\end{document}

and accounts.dat is

date account1 account2 account3
2008-01-03 60   1200 400
2008-02-06 120  1600 410
2008-03-15 -10  1600 410
2008-04-01 1800 500 410
2008-05-20 2300 500 410
2008-06-15 800 1920 410

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.