5

I want to create a graph with dates on the x-axis. I started with the dateplot library in pgfplots. I want date labels only for every third date, e.g., March 4, March 7, March 10, etc., but I can't quite figure out how to do it. I looked at similar questions on here, but I'm still confused, and I would really appreciate some help. My code follows:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=6in,compat=newest}
\usepgfplotslibrary{dateplot}

\begin{document}

    \begin{flushleft}

        \begin{tikzpicture}
            \begin{axis}[
                date coordinates in=x,
                xtick=data,
                xticklabel={\month/\day},
                xtick distance=7,
            ]
                \addplot[color=black,mark=*] coordinates {
                    (2020-03-04,7)
                    (2020-03-05,11)
                    (2020-03-06,13)
                    (2020-03-07,14)
                    (2020-03-08,14)
                    (2020-03-09,16)
                    (2020-03-10,17)
                    (2020-03-11,27)
                    (2020-03-12,32)
                    (2020-03-13,40)
                    (2020-03-14,53)
                    (2020-03-15,69)
                    (2020-03-16,94)
                    (2020-03-17,144)
                    (2020-03-18,190)
                    (2020-03-19,231)
                    (2020-03-20,292)
                    (2020-03-21,351)
                    (2020-03-22,409)
                    (2020-03-23,536)
                    (2020-03-24,662)
                    (2020-03-25,799)
                    (2020-03-26,1216)
                    (2020-03-27,1465)
                    (2020-03-28,1804)
                    (2020-03-29,2136)
                    (2020-03-30,2474)
                    (2020-03-31,3011)
                    (2020-04-01,3518)
                    (2020-04-02,4045)
                    (2020-04-03,4566)
                    (2020-04-04,5277)
                    (2020-04-05,5940)
                    (2020-04-06,6360)
                    (2020-04-07,6910)
                    (2020-04-08,7530)
            };
            \end{axis}
        \end{tikzpicture}

    \end{flushleft}

\end{document}

The code produces this graph:enter image description here

But I want something with an x-axis like the one on this graph that I made in Excel:enter image description here.

1
6

You can test whether \ticknum is divisible by 7, and only print the tick if that is the case.

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=6in,compat=newest}
\usepgfplotslibrary{dateplot}

\begin{document}

    \begin{flushleft}

        \begin{tikzpicture}
            \begin{axis}[
                date coordinates in=x,
                xtick=data,
                xticklabel={\pgfmathtruncatemacro{\itest}{%
                (int(\ticknum/7)==\ticknum/7 ? 1 :0)}%
                \ifnum\itest=1\relax
                \pgfcalendarmonthshortname{\month}/\day
                \fi},
                xtick distance=7,
            ]
                \addplot[color=black,mark=*] coordinates {
                    (2020-03-04,7)
                    (2020-03-05,11)
                    (2020-03-06,13)
                    (2020-03-07,14)
                    (2020-03-08,14)
                    (2020-03-09,16)
                    (2020-03-10,17)
                    (2020-03-11,27)
                    (2020-03-12,32)
                    (2020-03-13,40)
                    (2020-03-14,53)
                    (2020-03-15,69)
                    (2020-03-16,94)
                    (2020-03-17,144)
                    (2020-03-18,190)
                    (2020-03-19,231)
                    (2020-03-20,292)
                    (2020-03-21,351)
                    (2020-03-22,409)
                    (2020-03-23,536)
                    (2020-03-24,662)
                    (2020-03-25,799)
                    (2020-03-26,1216)
                    (2020-03-27,1465)
                    (2020-03-28,1804)
                    (2020-03-29,2136)
                    (2020-03-30,2474)
                    (2020-03-31,3011)
                    (2020-04-01,3518)
                    (2020-04-02,4045)
                    (2020-04-03,4566)
                    (2020-04-04,5277)
                    (2020-04-05,5940)
                    (2020-04-06,6360)
                    (2020-04-07,6910)
                    (2020-04-08,7530)
            };
            \end{axis}
        \end{tikzpicture}

    \end{flushleft}

\end{document}

enter image description here

3
  • Thank you! This works perfectly. Do you know if there's a way to display the month with a three-letter abbreviation, e.g., 03/04 as Mar 04? Should I ask this as a separate question? – bclzyjhr Apr 10 '20 at 20:49
  • 1
    @bclzyjhr Sure, with \pgfcalendarmonthshortname{\month}. I added this to the code. You can also use \pgfcalendarmonthshortname{\month}~\day if you do not like the slash. – user194703 Apr 10 '20 at 21:03
  • Thank you so much! It works like a charm. – bclzyjhr Apr 10 '20 at 21:11
4

Remove xtick=data from your code, and add in some minor ticks if you want ticks on each day. Thanks to Schrödinger's cat for the abbreviated month names.

enter image description here

\documentclass[12pt]{article}
\usepackage{pgfplots}
\pgfplotsset{width=6in,compat=newest}
\usepgfplotslibrary{dateplot}

\begin{document}

    \begin{flushleft}

        \begin{tikzpicture}
            \begin{axis}[
                date coordinates in=x,
                xticklabel={\pgfcalendarmonthshortname{\month}/\day},
                xtick distance=3,
                minor x tick num=2,
               x tick label style={rotate=90}
            ]
                \addplot[color=black,mark=*] coordinates {
                    (2020-03-04,7)
                    (2020-03-05,11)
                    (2020-03-06,13)
                    (2020-03-07,14)
                    (2020-03-08,14)
                    (2020-03-09,16)
                    (2020-03-10,17)
                    (2020-03-11,27)
                    (2020-03-12,32)
                    (2020-03-13,40)
                    (2020-03-14,53)
                    (2020-03-15,69)
                    (2020-03-16,94)
                    (2020-03-17,144)
                    (2020-03-18,190)
                    (2020-03-19,231)
                    (2020-03-20,292)
                    (2020-03-21,351)
                    (2020-03-22,409)
                    (2020-03-23,536)
                    (2020-03-24,662)
                    (2020-03-25,799)
                    (2020-03-26,1216)
                    (2020-03-27,1465)
                    (2020-03-28,1804)
                    (2020-03-29,2136)
                    (2020-03-30,2474)
                    (2020-03-31,3011)
                    (2020-04-01,3518)
                    (2020-04-02,4045)
                    (2020-04-03,4566)
                    (2020-04-04,5277)
                    (2020-04-05,5940)
                    (2020-04-06,6360)
                    (2020-04-07,6910)
                    (2020-04-08,7530)
            };
            \end{axis}
        \end{tikzpicture}

    \end{flushleft}

\end{document}
2
  • Thank you! I had not used that earlier because I wanted to have tick marks at every date, but I think your method with minor ticks in between looks best. – bclzyjhr Apr 10 '20 at 20:55
  • 1
    @bclzyjhr I did not realize when I posted that you wanted ticks on each day. I've edited. – Torbjørn T. Apr 10 '20 at 21:03

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.