是否可以在横轴上为同一个图形添加第二个刻度?我想将日期与自参考日期以来的天数结合起来。插图应该可以清楚地说明我的意思:
我目前拥有的代码:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.9}
\usepackage{csvsimple}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
date,days,value
2015-01-01, 0, 3.2
2015-01-06, 5, 6.5
2015-01-15, 14, 6.8
2015-02-01, 31, 2.2
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
xmin=2015-01-01,
xmax=2015-02-01,
date coordinates in=x,
xticklabel={\day-\month-\year},
x tick label style={rotate=45,anchor=north east},
grid=both,
ymin=-1,ymax=10,
xlabel={Date ($day-month-year$)},
ylabel={Value},
]
\addplot table [x=date, y=value, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
通过添加带有不可见线的第二个 y 轴找到了答案;但也许有更优雅的方法来做到这一点:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.9}
\usepackage{csvsimple}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
date,days,value
2015-01-01, 0, 3.2
2015-01-06, 5, 6.5
2015-01-15, 14, 6.8
2015-02-01, 31, 2.2
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
date coordinates in=x,
xticklabel={\day-\month-\year},
x tick label style={rotate=45,anchor=north east},
grid=both,
ymin=-1,ymax=10,
enlarge x limits=false,
xlabel={Date ($day-month-year$)},
ylabel={Value},
]
\addplot table [x=date, y=value, col sep=comma] {data.csv};
\end{axis}
\begin{axis}[
hide y axis,
axis x line*=top,
enlarge x limits=false,
xlabel={Days since 01-01-2015},
]
\addplot [opacity=0] table [x=days, y=value, col sep=comma] {data.csv};
\end{axis}
\end{tikzpicture}
\end{document}