我有以下 MWE,我想将文件 myintervals.csv 中的数据作为填充区域放置在条形图后面。此处的可扩展宏存在问题,但我无法使其正常工作:
\documentclass[tikz]{standalone}
\usepackage{luatex85}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgfcalendar}
\usepackage{filecontents}
\usepgfplotslibrary{dateplot}
\begin{filecontents*}{mydata.csv}
monthlyTimeString;numberOfBypassTrips;numberOfBatteryTrips;numberOfMainsTrips
2016-11-30;12;0;3
2016-12-31;0;0;0
2017-01-31;0;3;0
2017-02-28;0;0;0
2017-03-31;0;4;0
2017-04-30;0;3;5
2017-05-31;8;0;0
2017-06-30;0;4;0
\end{filecontents*}
\begin{filecontents*}{myintervals.csv}
type,startDate,stopDate
commissioning,2016-11-15,2017-01-15
service,2017-05-15,2017-07-15
\end{filecontents*}
\begin{document}
\pgfplotstableread[col sep=semicolon,string type]{mydata.csv}\data
\pgfplotstableread[col sep=comma,string type]{myintervals.csv}\intervals
\begin{tikzpicture}
\begin{axis}[
ymin=0,
date coordinates in=x,
xticklabel={\month/\year},
ybar stacked,
x tick label style={rotate=90,anchor=east},
xlabel=Date,
xlabel near ticks,
ylabel=Number of occurences,
bar width=10pt,
xtick=data,
before end axis/.code={
\pgfplotstablegetrowsof{\intervals}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\pgfplotsforeachungrouped \row in {0,...,\rows}{
\pgfplotstablegetelem{\row}{type}\of\intervals
\edef\type{\pgfplotsretval}
\pgfplotstablegetelem{\row}{startDate}\of\intervals
\edef\startdate{\pgfplotsretval}
\pgfplotstablegetelem{\row}{stopDate}\of\intervals
\edef\stopdate{\pgfplotsretval}
\node[gray,anchor=north west,font={\footnotesize}] at ({axis cs:\startdate,0}|-{rel axis cs:0,1}) {\type};
\fill[opacity=0.5,gray!50] ({axis cs:\startdate,0}|-{rel axis cs:0,0}) rectangle ({axis cs:\stopdate,0}|-{rel axis cs:0,1});
}
},
]
\addplot table[x=monthlyTimeString,y=numberOfBypassTrips]{\data};
\addplot table[x=monthlyTimeString,y=numberOfBatteryTrips]{\data};
\addplot table[x=monthlyTimeString,y=numberOfMainsTrips]{\data};
\end{axis}
\end{tikzpicture}
\end{document}
输出如下所示:
当它看起来应该是这样的:
答案1
正如您所说,这是一个扩展问题。
\documentclass[tikz]{standalone}
\usepackage{luatex85}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgfcalendar}
\usepackage{filecontents}
\usepgfplotslibrary{dateplot}
\begin{filecontents*}{mydata.csv}
monthlyTimeString;numberOfBypassTrips;numberOfBatteryTrips;numberOfMainsTrips
2016-11-30;12;0;3
2016-12-31;0;0;0
2017-01-31;0;3;0
2017-02-28;0;0;0
2017-03-31;0;4;0
2017-04-30;0;3;5
2017-05-31;8;0;0
2017-06-30;0;4;0
\end{filecontents*}
\begin{filecontents*}{myintervals.csv}
type,startDate,stopDate
commissioning,2016-11-01,2017-01-01
service,2017-05-01,2017-06-31
\end{filecontents*}
\begin{document}
\pgfplotstableread[col sep=semicolon,string type]{mydata.csv}\data
\pgfplotstableread[col sep=comma,string type]{myintervals.csv}\intervals
\begin{tikzpicture}
\begin{axis}[
ymin=0,
date coordinates in=x,
xticklabel={\month/\year},
ybar stacked,
x tick label style={rotate=90,anchor=east},
xlabel=Date,
xlabel near ticks,
ylabel=Number of occurences,
bar width=10pt,
xtick=data,
before end axis/.code={
\pgfplotstablegetrowsof{\intervals}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}
\pgfplotsforeachungrouped \row in {0,...,\rows}{
\pgfplotstablegetelem{\row}{type}\of\intervals
\edef\type{\pgfplotsretval}
\pgfplotstablegetelem{\row}{startDate}\of\intervals
\edef\startdate{\pgfplotsretval}
\pgfplotstablegetelem{\row}{stopDate}\of\intervals
\edef\stopdate{\pgfplotsretval}
\edef\temp{\noexpand\node[gray,anchor=north west,font={\noexpand\footnotesize}] at
({axis cs:\startdate,0}|-{rel axis cs:0,1}) {\type};
\noexpand\fill[opacity=0.5,gray!50]
({axis cs:\startdate,0}|-{rel axis cs:0,0})
rectangle ({axis cs:\stopdate,0}|-{rel axis cs:0,1});
}
\temp
}
},
]
\addplot table[x=monthlyTimeString,y=numberOfBypassTrips]{\data};
\addplot table[x=monthlyTimeString,y=numberOfBatteryTrips]{\data};
\addplot table[x=monthlyTimeString,y=numberOfMainsTrips]{\data};
\end{axis}
\end{tikzpicture}
\end{document}