5

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.

2
  • 1
    What does your complete dataset look like? (Could you pastebin it or something?) – olagjo Dec 3 '14 at 18:46
  • Here is the same code as above, but with all the data: pastebin.com/v4uyA2nh – soapygopher Dec 3 '14 at 19:31
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 up `weird 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.

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.