Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I modify the following example from PGFplots package to have the x-axis represent months and labelled as JAN, FEB, MAR etc.?

\begin{tikzpicture}
    \begin{axis}[
        smooth,
        stack plots=y,
        area style,
        enlarge x limits=false]
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)} 
        \closedcycle;
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)}
        \closedcycle;
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)}
        \closedcycle;
    \end{axis}
\end{tikzpicture}
share|improve this question
add comment

1 Answer

up vote 9 down vote accepted

If you just want the labels to be shown as months (rather than also using the months as coordinates), you can define a list that holds the desired abbreviations and use them in the labels by using xticklabel={\pgfmathparse{<\listmacro>[Mod(\tick,12)]}\pgfmathresult}. The Mod operator makes sure that the list "wraps around".

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\def\monthnames{{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}}
\begin{tikzpicture}
    \begin{axis}[
        smooth,
        stack plots=y,
        area style,
        enlarge x limits=false,
        xtick=data,
        xticklabel={\pgfmathparse{\monthnames[Mod(\tick,12)]}\pgfmathresult}]
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)} 
        \closedcycle;
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)}
        \closedcycle;
    \addplot coordinates
        {(0,1) (1,1) (2,2) (3,2)}
        \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}

pgfplots with month names

If you want to use the month names as symbolic coordinates, you can use symbolic x coords={<list>}. The coordinates then have to be provided as (<month>,<value>):

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        smooth,
        stack plots=y,
        area style,
        enlarge x limits=false,
        symbolic x coords={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec},
        xtick=data]
    \addplot coordinates
        {(Jan,1) (Feb,1) (Mar,2) (Apr,2)} 
        \closedcycle;
    \addplot coordinates
        {(Jan,1) (Feb,1) (Mar,2) (Apr,2)}
        \closedcycle;
    \addplot coordinates
        {(Jan,1) (Feb,1) (Mar,2) (Apr,2)}
        \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}

And finally, you can use "real" dates using the library \pgfplotslibrary{dateplot}. The x coordinates then have to be in the form YYYY-MM-DD, and you can get quite powerful date formatting options for the tick labels by wrapping them in a pgfcalendar. To use the abbreviated months as labels, you would use xticklabel={\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{m}{.}}}, where the . represents abbreviated textual labels. t would be the full month names, - would be a numerical representation without leading zeros, 0 would be a numerical representation with leading zeros. Details can be found in the pgfmanual in section 57.2, Typesetting Calendars

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        smooth,
        stack plots=y,
        area style,
        enlarge x limits=false,
        date coordinates in=x,
        xtick=data,
        xticklabel={\pgfcalendar{tickcal}{\tick}{\tick}{\pgfcalendarshorthand{m}{.}}}]
    \addplot coordinates
        {(2011-1-1,1) (2011-2-1,1) (2011-3-1,2) (2011-4-1,2)} 
        \closedcycle;
    \addplot coordinates
        {(2011-1-1,1) (2011-2-1,1) (2011-3-1,2) (2011-4-1,2)}
        \closedcycle;
    \addplot coordinates
        {(2011-1-1,1) (2011-2-1,1) (2011-3-1,2) (2011-4-1,2)}
        \closedcycle;
    \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
@Jake Thanks very much for your quick answer and also your comment to change the title of the question. I would appreciate it if you can edit the title as you may wish. –  Yiannis Lazarides Jun 11 '11 at 7:44
    
@Jake I a getting this error any idea why? ! Package PGF Math Error: Unknown function `"Jan","Feb","Mar","Apr","May","Jun" ,"Jul","Aug","Sep","Oct","Nov","Dec"[0.0]'. –  Yiannis Lazarides Jun 11 '11 at 8:02
    
@Yiannis: Are you using a version of PGF that's older than 2.1? –  Jake Jun 11 '11 at 8:07
1  
@Jake You are right, I was trying it from work and we had an old distribution. Updated and it worked. –  Yiannis Lazarides Jun 11 '11 at 8:23
1  
@Jake Thanks, it all makes sense now. –  Yiannis Lazarides Jun 11 '11 at 10:41
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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