I would like to add several labels to a plot in pgfplots: one label for the month and one for the year, just like in this graph:

I tried to use the extra tick label, but I don't know how it is possible to change its format from month to year, so the extra and normal x label have the same format. Also I could not figure out how to get the ticks shifted so that they are left and right of the month label instead of centered.
MWE
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
extra x ticks={2009-10-01, 2010-02-01},
extra x tick style={yshift=-20pt},
%extra xticklabel=\year, % does not work
xticklabel style={anchor=near xticklabel},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}
\end{document}
EDIT
In order to get vertical lines between the different months labels, I tried to use Jakes approach and tried to name the nodes \year\month, e.g. 20102 (2010 Feb). The problem with that is that the months (\month) contain leading zeros and the parser parses them as octal numbers. So when I try to calculate \month-1 to get the node before the current one, I receive a parser error. In order to solve that I tried to use the methods provided here, but none of them worked so far.
MWE2
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\usetikzlibrary{calc}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}
\makeatother
\begin{document}
\def\monthnames{{1,2,3,4,5,6,7,8,9,10,11,12}}
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel style={
anchor=near xticklabel,
% Approach 1: Does not work missing endcsname error
%name=\ifnodedefined{start\year{\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{m}{-}}}}
%{end\year} % Then this could be the last month
%{start\year}, % Otherwise, start the year
% Approach 2: Does not work missing endcsname error
%name=\ifnodedefined{start\year\pgfmathparse{\monthnames[Mod(\tick-1,12)]}\pgfmathresult} % Does not work missing endcsname error
%{end\year} % Then this could be the last month
%{start\year}, % Otherwise, start the year
},
xticklabel=\month,
date ZERO=2009-08-01,% <- improves precision!
]
\addplot coordinates {
(2009-08-01, 050)
(2009-09-01, 100)
(2009-10-01, 100)
(2009-11-01, 100)
(2009-12-01, 040)
(2010-01-01, 020)
(2010-02-01, 000)
(2010-03-01, 035)
};
\end{axis}
\end{tikzpicture}
\end{document}


extra x tick styleoption. In your case writeextra x tick style={yshift=-20pt,xticklabel=\year}– Red Jun 21 '13 at 10:18