我的 CSV 如下所示:
year,min,0.1,0.5,0.75,max,mean,totals
1992,4,200,400,780,1500,600,2345
1993,8,400,800,1560,3000,1200,4680
我想要用 pgfplots 绘制一个箱线图,但是文档太长了,我没有时间花几天时间甚至来获取 MWE。有人可以提供一个我可以参考的 MWE 吗?
如果可能的话,我还想在每个图的平均值之间画一条趋势线,并添加一个我手动绘制的整数值(即不是来自 CSV)以显示异常值。
答案1
我不明白为什么total
箱线图需要该列,但无论如何,这是根据你的要求改编的这个答案,这个答案和这个这个答案。
代码
\documentclass[crop=false]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\makeatletter
\pgfplotsset{
boxplot prepared from table/.code={
\def\tikz@plot@handler{\pgfplotsplothandlerboxplotprepared}%
\pgfplotsset{
/pgfplots/boxplot prepared from table/.cd,
#1,
}
},
/pgfplots/boxplot prepared from table/.cd,
table/.code={\pgfplotstablecopy{#1}\to\boxplot@datatable},
row/.initial=0,
make style readable from table/.style={
#1/.code={
\pgfplotstablegetelem{\pgfkeysvalueof{/pgfplots/boxplot prepared from table/row}}{##1}\of\boxplot@datatable
\pgfplotsset{boxplot/#1/.expand once={\pgfplotsretval}}
}
},
make style readable from table=lower whisker,
make style readable from table=upper whisker,
make style readable from table=lower quartile,
make style readable from table=upper quartile,
make style readable from table=median,
make style readable from table=lower notch,
make style readable from table=upper notch
}
\makeatother
\pgfplotstableread[col sep = comma]{data.csv}\datatable
\begin{document}
\begin{tikzpicture}
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro\TotalRows{\pgfplotsretval-1}
\begin{axis}[boxplot/draw direction=y,
xticklabels from table = {\datatable}{year},
xtick = {1,...,\TotalRows+1}
]
\pgfplotsinvokeforeach{0,...,\TotalRows}
{
\addplot+[
boxplot prepared from table={
table=\datatable,
row=#1,
lower whisker=min,
upper whisker=max,
lower quartile=0.1,
upper quartile=0.75,
median=0.5
},
boxplot prepared]
coordinates {};
};
\addplot table [x expr =\coordindex+1,y index=6] {\datatable};
\node[label={225:{Outlier}},circle,fill,inner sep=1.5pt] at (axis cs:2,3000) {};
\legend{};
\end{axis}
\end{tikzpicture}
\end{document}