我在绘制时间序列时遇到了一个奇怪的问题。在第一个图中,一切正常。然而,在第二个图中,第一个 xtick 之前的点似乎被反向绘制了。我做错了什么?
\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
您需要添加date ZERO
,如 pgfplots 手册第 4.21 节中所写。
\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}