10

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}
Jake
  • 232,450
yannisl
  • 117,160

1 Answers1

15

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}
Jake
  • 232,450
  • @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. – yannisl Jun 11 '11 at 07: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]'. – yannisl Jun 11 '11 at 08:02
  • @Yiannis: Are you using a version of PGF that's older than 2.1? – Jake Jun 11 '11 at 08:07
  • 1
    @Jake You are right, I was trying it from work and we had an old distribution. Updated and it worked. – yannisl Jun 11 '11 at 08:23
  • @Jake I would appreciate it if you check this http://pastebin.com/yQiGhBMM, it appears that the two lines are drawn to different scales. What would you recommend? – yannisl Jun 11 '11 at 08:32
  • @Yiannis: It looks fine to me: http://imgur.com/hUruU The second plot is stacked on top of the first, so the values specified for the second plot are added to those of the first. If you don't want that, you have to remove the stack plots=y and the smooth option (which for some reason doesn't work properly with \closedcycle in unstacked plots). – Jake Jun 11 '11 at 10:17
  • @Jake Puzzled, the first plot's highest value is around 300, but if you read the scale it reads higher. – yannisl Jun 11 '11 at 10:23
  • @Yiannis: The pink plot's highest value is 1041; the orange plot's highest value is 299, and because the orange plot is stacked on the pink one, it ends up at 1041+299=1350. – Jake Jun 11 '11 at 10:27
  • @Yiannis: Side note: Instead of repeating the month name list for series longer than one year, you can use xticklabel={\pgfmathparse{\monthnames[Mod(\tick-1,12)]}\pgfmathresult}] to map the numbers to months. I've uploaded an edited version of your code: http://pastebin.com/4Gm90WxD – Jake Jun 11 '11 at 10:33