我想根据日期绘制一些数据,但由于日期范围很大(4-5 年),我想在表格中使用刻度dd.mm\\yyyy
,可以吗?最好不要重复每个刻度的年份,只显示年份变化,但这不是必要的 ;)
这是一个工作示例:
\begin{tikzpicture}
\begin{axis}[
axis on top,
enlarge x limits = false,
date coordinates in=x,
xticklabel=\day.\month.\year,
date ZERO = 2013-08-13
]
\addplot[blue] coordinates {
(2013-08-13, 5)
(2013-08-18, 10)
(2013-08-21, 4)
(2013-08-26, 7)
(2013-09-01, 1)
(2013-09-02, 0)
(2013-09-06, 4)
(2013-09-11, 5.5)
(2013-09-15, 3)
(2013-11-13, 5)
(2013-11-18, 10)
(2013-11-21, 4)
(2013-12-26, 7)
(2014-01-01, 1)
(2014-01-02, 0)
(2014-01-06, 4)
(2014-02-11, 5.5)
(2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}
我尝试替换xticklabel=\day.\month.\year
,但xticklabel={\day.\month\\ \year}
没有成功。
答案1
如同轴标签和TikZ 节点概述,引入换行符的一种方法是通过设置明确指定文本对齐方式
xticklabel style={align=center}
为了仅在年份发生变化时打印,您可以将前一个标签的年份值存储在宏中,并比较当前年份标签是否等于该年份:
xticklabel={
\day.\month.\\\ifx\year\prevyear\else\year\fi%
\xdef\prevyear{\year}%
}
完整代码
\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\edef\prevyear{}
\begin{axis}[
axis on top,
enlarge x limits = false,
date coordinates in=x,
xticklabel={
\day.\month.\\\ifx\year\prevyear\else\year\fi%
\xdef\prevyear{\year}%
},
xticklabel style={align=center},
date ZERO = 2013-08-13
]
\addplot[black] coordinates {
(2013-08-13, 5)
(2013-08-18, 10)
(2013-08-21, 4)
(2013-08-26, 7)
(2013-09-01, 1)
(2013-09-02, 0)
(2013-09-06, 4)
(2013-09-11, 5.5)
(2013-09-15, 3)
(2013-11-13, 5)
(2013-11-18, 10)
(2013-11-21, 4)
(2013-12-26, 7)
(2014-01-01, 1)
(2014-01-02, 0)
(2014-01-06, 4)
(2014-02-11, 5.5)
(2014-02-15, 3)
};
\end{axis}
\end{tikzpicture}
\end{document}