3

Assumed we have the following code:


Minimum Working Example (MWE):

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepgfplotslibrary{dateplot}

\begin{filecontents}{data.csv}
    Date;                   Value
    2019-04-01 12:00:00;    1
    2019-04-02 12:00:00;    2
    2019-04-03 12:00:00;    3
    2019-04-04 12:00:00;    4
    2019-04-05 12:00:00;    5
\end{filecontents}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[date coordinates in = x,
                     xmin                = 2019-04-02 12:00:00,
                     xticklabel          = \month-\day,
                     table/col sep       = semicolon]
                     \addplot table[x=Date,y=Value]{data.csv};
        \end{axis}
    \end{tikzpicture}%
\end{document}

Screenshot of the result:

Screenshot of the result


Question:

  • How can I replace the current numeric dates with weekdays names like Mon., Tue., Wed., Thu., Fri., Sat., Sun.?
  • Is there an option available so pgfplots can calculate the corresponding names of the days of the week for each numeric date automatically by itself?

I want to avoid setting xtick labels = {Mon., Tue., Wed., ...} manually by hand for each tick.

1
  • 1
    Like this? – user121799 May 2 '19 at 17:51
4

Looks like you can use the macros provided by the pgfcalendar package to convert \year-\month-\day to Julian days (\pgfcalendardatetojulian), then to day of week (\pgfcalendarjuliantoweekday) and then print the corresponding weekday (\pgfcalendarweekdayshortname), all directly in xticklabel:

xticklabel          = \pgfcalendardatetojulian{\year-\month-\day}{\tmpCnt}\pgfcalendarjuliantoweekday{\tmpCnt}{\tmpCnt}\pgfcalendarweekdayname{\tmpCnt},

\newcnt\tmpCnt is required first.

I suggest also adding xtick distance=1 so you get just one tick per day.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepgfplotslibrary{dateplot}

\begin{filecontents}{data.csv}
    Date;                   Value
    2019-04-01 12:00:00;    1
    2019-04-02 12:00:00;    2
    2019-04-03 12:00:00;    3
    2019-04-04 12:00:00;    4
    2019-04-05 12:00:00;    5
\end{filecontents}
\newcount\tmpCnt
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[date coordinates in = x,
                     xmin                = 2019-04-02 12:00:00,
                     xticklabel          = \pgfcalendardatetojulian{\year-\month-\day}{\tmpCnt}\pgfcalendarjuliantoweekday{\tmpCnt}{\tmpCnt}\pgfcalendarweekdayshortname{\tmpCnt},
                     xtick distance      = 1,
                     table/col sep       = semicolon]
                     \addplot table[x=Date,y=Value]{data.csv};
        \end{axis}
    \end{tikzpicture}%
\end{document}

enter image description here

0

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.