1

I ran into a weird issue when plotting a time-series. In the first plot, everything works fine. In the second plot, however, the points before the first xtick seem to be plotted backwards. What am I doing wrong?

Example plot

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    compat=1.16
}
\usepgfplotslibrary{dateplot}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    date coordinates in = x,
    xticklabel style = {rotate = 30, anchor = north east},
]
\addplot+ table [
    col sep = comma,
    x = datetime,
    y = temperature,
] {temperature.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
    date coordinates in = x,
    xticklabel style = {rotate = 30, anchor = north east},
    xtick = {
        2019-09-17,
        2019-09-18,
        2019-09-19
    },
]
\addplot+ table [
    col sep = comma,
    x = datetime,
    y = temperature,
] {temperature.dat};
\end{axis}
\end{tikzpicture}
\end{document}
datetime,temperature
2019-09-16 15:00:00,23.16315384615385
2019-09-16 16:00:00,23.143547169811324
2019-09-16 17:00:00,22.819166666666657
2019-09-16 18:00:00,22.665943396226414
2019-09-16 19:00:00,22.524074074074065
2019-09-16 20:00:00,22.327351851851844
2019-09-16 21:00:00,22.055399999999988
2019-09-16 22:00:00,21.858518518518512
2019-09-16 23:00:00,21.68958181818182
2019-09-17 00:00:00,21.56796296296297
2019-09-17 01:00:00,21.460444444444445
2019-09-17 02:00:00,21.351629629629624
2019-09-17 03:00:00,21.25550909090908
2019-09-17 04:00:00,21.159444444444446
2019-09-17 05:00:00,21.1452909090909
2019-09-17 06:00:00,21.89207407407407
2019-09-17 07:00:00,22.93842592592592
2019-09-17 08:00:00,22.316981481481484
2019-09-17 09:00:00,23.08466037735848
2019-09-17 10:00:00,23.504370370370367
2019-09-17 11:00:00,23.426584905660366
2019-09-17 12:00:00,23.305454545454545
2019-09-17 13:00:00,23.08659259259259
2019-09-17 14:00:00,22.753166666666658
2019-09-17 15:00:00,22.512218181818174
2019-09-17 16:00:00,22.557566037735846
2019-09-17 17:00:00,22.3393396226415
2019-09-17 18:00:00,21.815672727272723
2019-09-17 19:00:00,21.519555555555556
2019-09-17 20:00:00,21.40996363636363
2019-09-17 21:00:00,21.143277777777776
2019-09-17 22:00:00,20.917226415094337
2019-09-17 23:00:00,20.730055555555552
  • 1
    as much as I can tell you did not have the data for 2019-09-19, so that could be a reason for such change in display of your time-series. If you change: xtick = { 2019-09-16, 2019-09-17, 2019-09-18 },, it should work. – Raaja Sep 23 '19 at 8:26
2

You need to add the date ZERO, as is written in section 4.21 of the pgfplots manual.

\documentclass[tikz,border=3]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{temperature.dat}
datetime,temperature
2019-09-16 15:00:00,23.16315384615385
2019-09-16 16:00:00,23.143547169811324
2019-09-16 17:00:00,22.819166666666657
2019-09-16 18:00:00,22.665943396226414
2019-09-16 19:00:00,22.524074074074065
2019-09-16 20:00:00,22.327351851851844
2019-09-16 21:00:00,22.055399999999988
2019-09-16 22:00:00,21.858518518518512
2019-09-16 23:00:00,21.68958181818182
2019-09-17 00:00:00,21.56796296296297
2019-09-17 01:00:00,21.460444444444445
2019-09-17 02:00:00,21.351629629629624
2019-09-17 03:00:00,21.25550909090908
2019-09-17 04:00:00,21.159444444444446
2019-09-17 05:00:00,21.1452909090909
2019-09-17 06:00:00,21.89207407407407
2019-09-17 07:00:00,22.93842592592592
2019-09-17 08:00:00,22.316981481481484
2019-09-17 09:00:00,23.08466037735848
2019-09-17 10:00:00,23.504370370370367
2019-09-17 11:00:00,23.426584905660366
2019-09-17 12:00:00,23.305454545454545
2019-09-17 13:00:00,23.08659259259259
2019-09-17 14:00:00,22.753166666666658
2019-09-17 15:00:00,22.512218181818174
2019-09-17 16:00:00,22.557566037735846
2019-09-17 17:00:00,22.3393396226415
2019-09-17 18:00:00,21.815672727272723
2019-09-17 19:00:00,21.519555555555556
2019-09-17 20:00:00,21.40996363636363
2019-09-17 21:00:00,21.143277777777776
2019-09-17 22:00:00,20.917226415094337
2019-09-17 23:00:00,20.730055555555552
\end{filecontents*}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    date coordinates in = x,
    xticklabel style = {rotate = 30, anchor = north east},
]
\addplot+ table [
    col sep = comma,
    x = datetime,
    y = temperature,
] {temperature.dat};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
    date coordinates in = x,
    xticklabel style = {rotate = 30, anchor = north east},
    xtick = {
        2019-09-17,
        2019-09-18,
        2019-09-19
    },date ZERO=2019-09-16
]
\addplot+ table [
    col sep = comma,
    x = datetime,
    y = temperature,
] {temperature.dat};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • why not just use: xtick = { 2019-09-16, 2019-09-17, 2019-09-18 } without date ZERO? – Raaja Sep 23 '19 at 16:10
  • 1
    @Raaja Thanks! The reason is that, according to my interpretation, the OP only wants two ticks. (Another reason is that I want to express that this is not a bug.) – Schrödinger's cat Sep 23 '19 at 16:42
  • ok thanks for the clarification – Raaja Sep 23 '19 at 17:04
  • When not set explicitly shouldn't date ZERO be set automatically to the first point? Why does that not work? – Claudio Sep 25 '19 at 10:53
  • @Claudio It does work. The first point is 2019-09-16 15:00:00, and not 2019-09-16. – Schrödinger's cat Sep 25 '19 at 14:59

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.