我的文件中有类似的数据。
1 0.1 0.2 0.3 0.8 ...
5 0.3 0.5 0.6 0.9 ...
10 0.2 0.3 0.7 0.10 ..
...
我如何告诉箱线图使用第一列作为绘制位置并计算其余数据的箱线图?
下面是一个例子,我想使用 5 和 8 作为绘图位置。
\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{
index lw lq med uq uw
5 5 7 8.5 9.5 10
8 4 5 6.5 8.5 9.5
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y]
\addplot+[
boxplot prepared from table={
table=\datatable,
lower whisker=lw,
upper whisker=uw,
lower quartile=lq,
upper quartile=uq,
median=med
}, boxplot prepared
]
coordinates {};
\addplot+[
boxplot prepared from table={
table=\datatable,
row=1,
lower whisker=lw,
upper whisker=uw,
lower quartile=lq,
upper quartile=uq,
median=med
}, boxplot prepared
]
coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您就只能使用该选项draw position
,请参阅手册第 5.12 节,第 470 页。
编辑的代码:
\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=draw position,
make style readable from table=upper notch
}
\makeatother
\pgfplotstableread{
index lw lq med uq uw
5 5 7 8.5 9.5 10
8 4 5 6.5 8.5 9.5
}\datatable
\begin{document}
\begin{tikzpicture}
\begin{axis}[boxplot/draw direction=y]
\addplot+[
boxplot prepared from table={
table=\datatable,
lower whisker=lw,
upper whisker=uw,
lower quartile=lq,
upper quartile=uq,
median=med
}, boxplot prepared={,},
]
coordinates {};
\addplot+[
boxplot prepared from table={
table=\datatable,
row=1,
lower whisker=lw,
upper whisker=uw,
lower quartile=lq,
upper quartile=uq,
draw position = index,
median=med,
}, boxplot prepared={},
]
coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}