I would like to plot quarterly time series with special x-axis features. In particular, I would like to see small ticks indicating when the quarters begin and larger ticks indicating when the years begin, but would only like to label the years (and not the quarters). For illustration purposes, the following example does exactly what I want, except that the code is not flexible at all.



date    Y
2009-02-15  1
2009-05-15  2
2009-08-15  3
2009-11-15  4
2010-02-15  4
2010-05-15  4
2010-08-15  4
2010-11-15  4
2011-02-15  4
2011-05-15  4
2011-08-15  4
2011-11-15  4
2012-02-15  4
2012-05-15  4
2012-08-15  4
2012-11-15  4
2013-02-15  4
2013-05-15  4
2013-08-15  4
2013-11-15  4

mark =none,
major tick length=4pt,
minor tick length=2pt,
date coordinates in=x,
minor x tick num=3,
xticklabels= {\year,\year,\year},
x tick label style={anchor=east,xshift=1.5cm,yshift=-0.3cm},
\addplot [only marks,green]table [x=date,y=Y]{Quarterly.dat}; %

The code is not flexible for several reasons. For instance, if I write xtick={2009-01-01,2010-01-01,2011-01-01,2012-01-01,2013-01-01}, the minor x tick num=3 stops working (probably because the ticks don’t have the same distance). The example also requires that I have to manually set xshift (in the example xshift=1.5cm). It would be great if the code could automatically lable all years for any quarterly time series. I also tried to use the extra x ticks option, but without success.


  • There is an x tick label as interval option. And replacing 2013-01-01 by 2013-01-00 fixes your problem. – Symbol 1 Dec 31 '14 at 8:48

Turns out this is a very good practice for x coord trafo/.code and x coord inv trafo/.code. Before that let me explain a little.

Your problem falls into the following three parts:

  • First, major ticks should appear at every new year eve. But dateplot knows nothing about new year.
  • Second, minor ticks appear only if major ticks are separated uniformly. But years are 365 or 366 days long.
  • Third, you want to control the layout.

There is a very easy way to get over it: make years as wide as each other. More precisely, I use 2015.09314 to represent today, Feb 3, 2015. This changes everything because:

  • New year eves are now represented by integers and pgfplots LOVES integer.
  • Years are one unit long.
  • Controlling layout is easier with something like xmin=2008.

So all you have to do is check out tikzlibrarypgfplots.dateplot.code.tex and write your own year coordinates in. In the following code, /pgfplots/#1 coord trafo is used to transform your input 2015-2-3 to a decimal number 2015.09314 so then pgfplots can plot data. On the other hand, x coord inv trafo is used to transform the decimal number to a label text. (For example MMXV instead of 2015.) (I did not do this one because the default is good enough.) (Well... I set 1000 sep to nothing in another syntax.)

date Y
2009-01-01 9
2009-12-31 9
2010-01-01 0
2010-12-31 0
2014-01-01 4
2014-12-31 4

    /pgfplots/year coordinates in/.code={
            #1 tick label style={/pgf/number format/1000 sep=}, % "2015" rather than "2,015"
            #1 tick label as interval,
            minor #1 tick num=11 % January, ..., December
        \pgfkeysdef{/pgfplots/#1 coord trafo}{
            % check if we also have a TIME like '2006-01-01 11:21'
                % we have a TIME!
            \advance\c@pgf@counta by-\c@pgf@countb % now a = #days from 1/1 to temp
            \advance\c@pgf@countc by-\c@pgf@countb % now b = #days of that year
                % no time:
                % add time fraction (which should be in the range
                % [0,1]).
                \ifdim\pgfplotstemptime pt<1pt
                    % discard prefix '0.':
                    % assume \pgfplotstemptime=1pt :
                    \advance\c@pgf@counta by1

        \begin{axis}[year coordinates in=x,minor x tick num=1]
            \addplot [only marks]table[x=date,y=Y]{\jobname-Quarterly.dat};

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.