![带有日期 x 轴的 tikz 图](https://linux22.com/image/474174/%E5%B8%A6%E6%9C%89%E6%97%A5%E6%9C%9F%20x%20%E8%BD%B4%E7%9A%84%20tikz%20%E5%9B%BE.png)
我正在制作一个 tikz 图,该图描绘了数据跨越约 20 年的时间序列。下面是我的简单乳胶代码,图本身位于底部。我需要解决的问题是:
- 如何使 x 轴标签具有 12 个月的间隔而不是 1 个月(每个数据点)?
- 如何添加一条红色垂直线来指示截至 2022 年 2 月 24 日的事件?
- 如何添加一个灰色矩形来表示 2022 年 2 月 24 日至 2024 年 2 月 27 日的区域?
我将非常感谢您的有益见解!
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.18}
\usetikzlibrary{pgfplots.dateplot}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{lipsum}
\begin{filecontents}{data.txt}
Date,Ind1,Ind2,Ind3,Ind4
2021-01-01,64,34,2,30
2021-02-01,65,34,2,31
2021-03-01,63,35,2,28
2021-04-01,65,33,2,32
2021-05-01,67,32,2,35
2021-06-01,66,32,2,34
2021-07-01,64,35,1,29
2021-08-01,61,37,2,24
2021-09-01,64,34,2,30
2021-10-01,67,33,1,34
2021-11-01,63,35,2,28
2021-12-01,65,34,1,31
2022-01-01,69,29,2,40
2022-02-01,71,27,1,44
2022-03-01,83,15,2,68
2022-04-01,82,17,1,65
2022-05-01,83,15,2,68
2022-06-01,83,16,1,67
2022-07-01,83,15,2,68
2022-08-01,83,15,2,68
2022-09-01,77,21,2,56
2022-10-01,79,19,2,60
2022-11-01,79,18,3,61
2022-12-01,81,17,2,64
2023-01-01,82,16,2,66
2023-02-01,83,14,2,69
2023-03-01,82,15,3,67
2023-04-01,83,14,3,69
2023-05-01,82,15,2,67
2023-06-01,81,16,3,65
2023-07-01,82,15,3,67
2023-08-01,80,16,4,64
2023-09-01,80,17,3,63
2023-10-01,82,15,3,67
2023-11-01,85,13,2,72
2023-12-01,83,14,3,69
2024-01-01,85,12,2,73
\end{filecontents}
\begin{document}
\section{Foo}\lipsum[1]
\begin{figure}[!ht]
\centering
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel style={rotate=90,anchor=near xticklabel},
xticklabel=\month.\year, % required, example: Jan-24
xtick distance=1000,
title={}, xlabel={},
y tick label style={/pgf/number format/1000 sep=},
extra y tick style={grid=major, tick label style={xshift=-1cm}},
ylabel={},
date ZERO=2009-08-18,% <- improves precision!
]
\addplot[black] table[col sep=comma,x=Date,y=Ind1] {data.txt};
\addplot[blue] table[col sep=comma,x=Date,y=Ind4] {data.txt};
\end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:enter-label}
\end{figure}
\end{document}
答案1
这是实现此目的的一种方法。
- 说出您想要哪些刻度、您已在哪里使用了
dateplot
库以及哪些 make 语句xtick=data,
已过时:
...
date ZERO=2009-08-18,% <- improves precision!
% ~~~ which major ticks to show ~~~
xtick={2021-01-01, 2022-01-01, 2023-01-01, 2024-01-01},
% ~~~ put 11 for months, or 3 for quarters in between ~~~
minor x tick num=3,
]
...
- 使用灰色区域的坐标并首先绘制它;即指定整个路径,该路径可能远非矩形:
% ~~~ gray area: draw fist ~~~~~~~~~~~~~~~~~~~
\addplot[fill=gray!30,draw=none] coordinates {
(2022-02-24,30)(2022-02-24,90)
(2024-02-27,90)(2024-02-27,30)
(2022-02-24,30)
};
- 红色垂直线也一样:
% ~~~ red vertical ~~~~~~~~~~~~~~~~~~~
\addplot[red] coordinates {(2022-02-24,15)(2022-02-24,100)};
建议:您可能还想使用注释,参见手册中的第 4.17 章,例如,指示红色和灰色日期。
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{width=12cm,compat=1.18}
\usetikzlibrary{pgfplots.dateplot}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{lipsum}
\begin{filecontents}{data.txt}
Date,Ind1,Ind2,Ind3,Ind4
2021-01-01,64,34,2,30
2021-02-01,65,34,2,31
2021-03-01,63,35,2,28
2021-04-01,65,33,2,32
2021-05-01,67,32,2,35
2021-06-01,66,32,2,34
2021-07-01,64,35,1,29
2021-08-01,61,37,2,24
2021-09-01,64,34,2,30
2021-10-01,67,33,1,34
2021-11-01,63,35,2,28
2021-12-01,65,34,1,31
2022-01-01,69,29,2,40
2022-02-01,71,27,1,44
2022-03-01,83,15,2,68
2022-04-01,82,17,1,65
2022-05-01,83,15,2,68
2022-06-01,83,16,1,67
2022-07-01,83,15,2,68
2022-08-01,83,15,2,68
2022-09-01,77,21,2,56
2022-10-01,79,19,2,60
2022-11-01,79,18,3,61
2022-12-01,81,17,2,64
2023-01-01,82,16,2,66
2023-02-01,83,14,2,69
2023-03-01,82,15,3,67
2023-04-01,83,14,3,69
2023-05-01,82,15,2,67
2023-06-01,81,16,3,65
2023-07-01,82,15,3,67
2023-08-01,80,16,4,64
2023-09-01,80,17,3,63
2023-10-01,82,15,3,67
2023-11-01,85,13,2,72
2023-12-01,83,14,3,69
2024-01-01,85,12,2,73
\end{filecontents}
\begin{document}
\section{Foo}\lipsum[1]
\begin{figure}[!ht]
\centering
\begin{tikzpicture}
\begin{axis}[
date coordinates in=x,
xtick=data,
xticklabel style={rotate=90,anchor=near xticklabel},
xticklabel={\month.\year}, % required, example: Jan-24
xtick distance=1000,
title={}, xlabel={},
y tick label style={/pgf/number format/1000 sep=},
extra y tick style={grid=major,
tick label style={xshift=-1cm}},
ylabel={},
date ZERO=2009-08-18,% <- improves precision!
% ~~~ which major ticks to show ~~~
xtick={2021-01-01, 2022-01-01, 2023-01-01, 2024-01-01},
% ~~~ put 11 for months, or 3 for quarters in between ~~~
minor x tick num=3,
]
% ~~~ gray area: draw fist ~~~~~~~~~~~~~~~~~~~
\addplot[fill=gray!30,draw=none] coordinates {
(2022-02-24,30)(2022-02-24,90)
(2024-02-27,90)(2024-02-27,30)
(2022-02-24,30)
};
% ~~~ your data ~~~~~~~~~~~
\addplot[black] table[col sep=comma,x=Date,y=Ind1] {data.txt};
\addplot[blue] table[col sep=comma,x=Date,y=Ind4] {data.txt};
% ~~~ red vertical ~~~~~~~~~~~~~~~~~~~
\addplot[red] coordinates {(2022-02-24,15)(2022-02-24,100)};
\end{axis}
\end{tikzpicture}
\caption{Caption}
\label{fig:enter-label}
\end{figure}
\end{document}