我正在绘制一个以周为单位的时间序列。最小示例:
\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
height=0.6\linewidth,
axis lines=left,
ymin=0,
ylabel={Price},
date coordinates in=x,
x tick label as interval,
xticklabel={\year}
]
\addplot
[draw=black, mark=none]
table {
2009-01-02 31.42
2009-01-09 29.64
2009-01-16 27.44
2009-01-23 27.92
2009-01-30 28.6
2009-02-06 28.16
2009-02-13 27.65
... more data ...
2014-10-24 36.51
2014-10-31 37.06
2014-11-07 37.61
2014-11-14 39.95
2014-11-21 41.76
2014-11-28 43.38
};
\end{axis}
\end{tikzpicture}
\end{document}
输出如下所示,其中包含多个同一年份的内容:
我想要 x 轴上每个间隔只有一个年份,并用刻度标记两年之间的边界,如下所示:
任何帮助是极大的赞赏。
答案1
我最终假装了。
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
height=0.6\linewidth,
axis lines=left,
ymin=0,
ylabel={Price (NOK)},
ylabel near ticks,
minor xtick=\empty,
xtick={52, 104, ..., 260},
xticklabel=\empty,
xmajorgrids=true,
]
\addplot
[draw=black, mark=none]
table {
0 31.42
1 29.64
2 27.44
3 27.92
4 28.60
...
306 39.95
307 41.76
308 43.38
};
\end{axis}
\foreach \year [count=\i] in {2009, 2010, ..., 2014}{
\node at (-1.1 + 2.2*\i, -1em) {\year};
}
\end{tikzpicture}
我将日期坐标改为一个简单的计数器,并手动放置年份标签。通过反复试验找到了标签的偏移量和增量。
这个版本还给了我一个奇怪的错误:
! Missing number, treated as zero. <to be read again> p l.330 \end{axis} A number should have been here; I inserted `0'. (If you can't figure out why I needed to see a number, look up `weird error' in the index to The TeXbook.)
行号指向哪里\end{axis}
,那么规范是否有问题axis
?有什么想法导致这种情况吗?
编辑:Missing number, treated as zero
如果我从轴选项中取出,错误就会消失minor xtick=\empty
。输出显然是相同的。