我不明白矩形的位置:我认为我必须从红线之后开始
我好像已经过去了24小时。
红线坐标为axis cs:2021-06-03 00:00
矩形开始于axis cs:2021-06-03 12:00
版本 :
- pdfTeX,版本 3.14159265-2.6-1.40.20
- \pgfplotsset{兼容性=1.16}
代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.dateplot}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[date coordinates in=x, grid,
width=\linewidth,
xticklabel style= {rotate=45,anchor=near xticklabel},
]
\addplot [smooth, mark=*] table [x, y=partiel,] {
date ok partiel
2021-06-18 74.17 77.50
2021-06-17 47.92 73.28
2021-06-16 79.18 81.57
2021-06-15 78.49 80.64
2021-06-14 76.25 79.24
2021-06-13 80.98 83.57
2021-06-12 35.82 39.63
2021-06-11 51.28 51.96
2021-06-10 79.49 81.57
2021-06-09 80.23 82.45
2021-06-08 80.20 82.16
2021-06-07 75.52 78.27
2021-06-06 82.20 84.80
2021-06-05 34.53 38.49
2021-06-04 75.20 78.64
2021-06-03 49.01 75.37
2021-06-02 74.34 82.34
2021-06-01 16.74 19.38
2021-05-31 3.44 4.01
};
\draw[draw=black, fill=black!40, opacity=.75]
(axis cs:2021-06-03 12:00, 0) rectangle (axis cs:2021-06-05 12:00, 100);
\draw[draw=red,] (axis cs:2021-06-03 00:00, -10) -- (axis cs:2021-06-03 00:00, 100);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我不知道为什么,但是您的反向绘图会干扰 PGFPlots 根据日期时间计算 x 值的方式。对日期进行排序可以更正绘图:
\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.dateplot}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[date coordinates in=x, grid,
width=\linewidth,
xticklabel style= {rotate=45,anchor=near xticklabel},
]
\addplot [smooth, mark=*] table [x, y=partiel,] {
date ok partiel
2021-05-31 3.44 4.01
2021-06-01 16.74 19.38
2021-06-02 74.34 82.34
2021-06-03 49.01 75.37
2021-06-04 75.20 78.64
2021-06-05 34.53 38.49
2021-06-06 82.20 84.80
2021-06-07 75.52 78.27
2021-06-08 80.20 82.16
2021-06-09 80.23 82.45
2021-06-10 79.49 81.57
2021-06-11 51.28 51.96
2021-06-12 35.82 39.63
2021-06-13 80.98 83.57
2021-06-14 76.25 79.24
2021-06-15 78.49 80.64
2021-06-16 79.18 81.57
2021-06-17 47.92 73.28
2021-06-18 74.17 77.50
};
\draw[draw=black, fill=black!40, opacity=.75]
(axis cs:2021-06-03 12:00, 0) rectangle (axis cs:2021-06-05 12:00, 100);
\draw[draw=red,] (axis cs:2021-06-03 00:00, -10) -- (axis cs:2021-06-03 00:00, 100);
\end{axis}
\end{tikzpicture}
\end{document}
编辑:
遇到的第一个日期用作date ZERO
(手册中的第 392 页)。这意味着当在反向图中首先看到最高日期时,所有其他日期都以某种方式为负数。可以通过date ZERO
明确设置来实现正确的结果:
\documentclass[border=1 cm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{pgfplots.dateplot}
\pgfplotsset{compat=newest, /pgfplots/date ZERO=2021-05-31}
\begin{document}
\begin{tikzpicture}
\begin{axis}[date coordinates in=x, grid,
width=\linewidth,
xticklabel style= {rotate=45,anchor=near xticklabel},
]
\addplot [smooth, mark=*] table [x, y=partiel,] {
date ok partiel
2021-06-18 74.17 77.50
2021-06-17 47.92 73.28
2021-06-16 79.18 81.57
2021-06-15 78.49 80.64
2021-06-14 76.25 79.24
2021-06-13 80.98 83.57
2021-06-12 35.82 39.63
2021-06-11 51.28 51.96
2021-06-10 79.49 81.57
2021-06-09 80.23 82.45
2021-06-08 80.20 82.16
2021-06-07 75.52 78.27
2021-06-06 82.20 84.80
2021-06-05 34.53 38.49
2021-06-04 75.20 78.64
2021-06-03 49.01 75.37
2021-06-02 74.34 82.34
2021-06-01 16.74 19.38
2021-05-31 3.44 4.01
};
\draw[draw=black, fill=black!40, opacity=.75]
(axis cs:2021-06-03 12:00, 0) rectangle (axis cs:2021-06-05 12:00, 100);
\draw[draw=red,] (axis cs:2021-06-03 00:00, -10) -- (axis cs:2021-06-03 00:00, 100);
\end{axis}
\end{tikzpicture}
\end{document}