1

I'm trying to get a plot with xticks with the first day of the month, as in this question and using abbreviated month names, as it is done here. This is what I've tried:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepgfplotslibrary{dateplot}
\def\monthnames{{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}}

\begin{document}
\def\dateticks{2016-Jan-01}
\foreach \i in {1,...,11}
    {\xdef\dateticks{\dateticks,2016-\pgfmathparse{\monthnames[Mod(\i,12)]}\pgfmathresult-01}}

\begin{tikzpicture}
\begin{axis}[
  date coordinates in=x,
  xtick/.expanded={\dateticks},
  xticklabel=\day/\month,
  xticklabel style={rotate=90},
]
\addplot coordinates {(2016-01-23,10) (2016-03-05,20) (2016-09-14,10)};
\end{axis}
\end{tikzpicture}
\end{document}

However, this produces the error: Incomplete \iffalse; all text was ignored after line 11. Not sure what the problem is here because I don't have (at least explicitly) a conditional statement.

Any ideas on how to generate the \dateticks macro with the abbreviated month names? What I would like it the following figure, but with the month names instead of numbers:

enter image description here

Thanks!

0

Maybe you want something like this?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepgfplotslibrary{dateplot}
\def\monthnames{{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}}
\def\dateticks{2016-Jan-01}
\foreach \i in {1,...,11}
    {\pgfmathparse{\monthnames[Mod(\i,12)]}%
    \xdef\dateticks{\dateticks,2016-\pgfmathresult-01}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  date coordinates in=x,
  xtick=data,
  xticklabels/.expanded=\dateticks,
  xticklabel style={rotate=90},
]
\addplot coordinates {(2016-01-23,10) (2016-03-05,20) (2016-09-14,10)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

OLDER ANSWER: If you want to use the month names from your list, you can use

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepgfplotslibrary{dateplot}
\def\monthnames{{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  date coordinates in=x,
  xticklabel=\pgfmathtruncatemacro\mmonth{\month}%
  \pgfmathsetmacro{\mymonth}{\monthnames[Mod(11+\mmonth,12)]}%
    \day/\mymonth,
  xticklabel style={rotate=90},
]
\addplot coordinates {(2016-01-23,10) (2016-03-05,20) (2016-09-14,10)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

However, the same result can be obtained with xticklabel=\day/\pgfcalendarmonthshortname{\month}.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  date coordinates in=x,
  xticklabel=\day/\pgfcalendarmonthshortname{\month},
  xticklabel style={rotate=90},
]
\addplot coordinates {(2016-01-23,10) (2016-03-05,20) (2016-09-14,10)};
\end{axis}
\end{tikzpicture}
\end{document}
2
  • Thanks @Schrödinger's cat! Do you know why the error in my code? I had a hard time finding why it wasn't working, any insights would be appreciated. – kmundnic Jun 9 '20 at 3:18
  • 2
    @kmundnic Yes. There are two issues. First, \pgfmathparse is not expandable, so you would need \foreach \i in {1,...,11} {\pgfmathparse{\monthnames[Mod(\i,12)]}\xdef\dateticks{\dateticks,2016-\pgfmathresult-01}}. Then the error when generating the list disappears. Second, if I am not mistaken, this list is not suitable for xtick/.expanded={\dateticks},. You could use xticklabel/.expanded={\dateticks}, but the result is probably not what you want. – user194703 Jun 9 '20 at 3:24

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.