6

I am plotting a time series with weekly resolution. Minimal example:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=\linewidth,
        height=0.6\linewidth,
        axis lines=left,
        ymin=0,
        ylabel={Price},
        date coordinates in=x,
        x tick label as interval,
        xticklabel={\year}
    ]

    \addplot 
    [draw=black, mark=none]
    table {
        2009-01-02 31.42
        2009-01-09 29.64
        2009-01-16 27.44
        2009-01-23 27.92
        2009-01-30 28.6
        2009-02-06 28.16
        2009-02-13 27.65
        ... more data ...
        2014-10-24 36.51
        2014-10-31 37.06
        2014-11-07 37.61
        2014-11-14 39.95
        2014-11-21 41.76
        2014-11-28 43.38
    };

    \end{axis}
\end{tikzpicture}

\end{document}

The output looks like this, with more than one of the same year:

enter image description here

I would like a single year in each interval on the x axis, with ticks to mark the boundary between two years, like this:

enter image description here

Any help is greatly appreciated.

Stefan Pinnow
  • 29,535

1 Answers1

3

I ended up faking it.

\begin{tikzpicture}
    \begin{axis}[
        width=\linewidth,
        height=0.6\linewidth,
        axis lines=left,
        ymin=0,
        ylabel={Price (NOK)},
        ylabel near ticks,
        minor xtick=\empty,
        xtick={52, 104, ..., 260},
        xticklabel=\empty,
        xmajorgrids=true,
    ]

    \addplot 
    [draw=black, mark=none]
    table {
        0   31.42
        1   29.64
        2   27.44
        3   27.92
        4   28.60
        ...
        306 39.95
        307 41.76
        308 43.38
    };
    \end{axis}

    \foreach \year [count=\i] in {2009, 2010, ..., 2014}{
        \node at (-1.1 + 2.2*\i, -1em) {\year};
    }
\end{tikzpicture}

I changed the date coordinates to a simple counter and placed the year labels manually. The offset and increment for the labels were found by trial and error.

enter image description here

This version also gives me a strange error:

! Missing number, treated as zero.
<to be read again> 
                   p
l.330     \end{axis}

A number should have been here; I inserted 0'. (If you can't figure out why I needed to see a number, look upweird error' in the index to The TeXbook.)

The line number points to where \end{axis} is, so is there something wrong with the axis specification?. Any ideas what's causing this?

Edit: The Missing number, treated as zero error goes away if I take out minor xtick=\empty from the axis options. The output is apparently the same.