使用 pgfplots 创建箱线图时,它会默认绘制一个修改后的箱线图,其中须是最大/最小的非异常值值和异常值用点表示。有没有办法将它们简单地设置为最大/最小值,而不必用 明确指定它们boxplot prepared
?
梅威瑟:
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepgfplotslibrary{statistics}
\begin{filecontents}{data.txt}
497
222
849
480
477
662
1005
460
1131
540
506
867
706
1558
954
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\linewidth, height=4cm, enlarge y limits=.2,
ytick={1, 2}, yticklabels={Wrong, Right}]
\addplot+ [boxplot] table [y index=0] {data.txt};
% Ignore the miscalculations here
\addplot+ [boxplot prepared={draw position=2,
median=662,
lower whisker=222,
upper whisker=1558,
upper quartile=867,
lower quartile=478.5}] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
一个答案是将 设置whisker range
为一个非常高的值,它决定了哪些点被视为异常值。
\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{width=\textwidth, compat=1.12}
\usepgfplotslibrary{statistics}
%% A value larger than the ratio between any quartile range of any
%% boxplot in the document.
\newcommand\boxplotbignum{1000000}
\begin{filecontents}{data.txt}
497
222
849
480
477
662
1005
460
1131
540
506
867
706
1558
954
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=\linewidth, height=4cm, enlarge y limits=.2,
ytick={1, 2}, yticklabels={Wrong, Right}]
\addplot+ [boxplot={whisker range=\boxplotbignum}] table [y index=0] {data.txt};
% Ignore the miscalculations here
\addplot+ [boxplot prepared={draw position=2,
median=662,
lower whisker=222,
upper whisker=1558,
upper quartile=867,
lower quartile=478.5}] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}