我想在我的箱线图中包括平均值。箱线图使用https://tex.stackexchange.com/a/183856/164280举个例子。我见过如何使用 pgfplots 在箱线图上显示平均值或平均值使用 是可能的boxplot prepared
,但我使用boxplot
时出现错误unknown key
。有效的代码是:
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm, top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11,
/pgfplots/ybar legend/.style={
/pgfplots/legend image code/.code={%
\draw[##1,/tikz/.cd,yshift=-0.25em]
(0cm,0cm) rectangle (3pt,0.8em);},},}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={PR\textsubscript{A}},
axis lines*=left,
ymin = 0.75, ymax = 0.95,
ymajorgrids,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=2,
/tikz/.cd
},
height=8cm,
boxplot={
%
% Idea:
% place the
% group 1 at 0.3333 and 0.6666
% group 2 at 1.3333 and 1.6666
% group 3 at 2.3333 and 2.6666
% ...
% in a formular:
draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
%
% that means the box extend must be at most 0.33333 :
box extend=0.3,
},
% ... it also means that 1 unit in x controls the width:
x=3.2cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
{System 1\\{\small without/with}},%
{System 2\\{\small without/with}},%
{System 3\\{\small without/with}},%
{System 4\\{\small without/with}},%
},
x tick label style={
text width=5cm,
align=center
},
]
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.926\\
0.940\\
0.862\\
0.849\\
0.871\\
0.894\\
0.903\\
0.885\\
0.892\\
0.837\\
0.814\\
0.818\\
0.810\\
};
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black, fill = black!10},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.944\\
0.892\\
0.857\\
0.904\\
0.938\\
0.930\\
0.907\\
0.910\\
0.874\\
0.857\\
0.854\\
0.853\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.921\\
0.857\\
0.867\\
0.886\\
0.880\\
0.886\\
0.888\\
0.893\\
0.826\\
0.818\\
0.857\\
0.848\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black, fill = black!10},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.913\\
0.916\\
0.881\\
0.869\\
0.917\\
0.919\\
0.909\\
0.905\\
0.907\\
0.859\\
0.858\\
0.892\\
0.891\\
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.915\\
0.912\\
0.840\\
0.845\\
0.873\\
0.868\\
0.875\\
0.877\\
0.883\\
0.812\\
0.789\\
0.840\\
0.833\\
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black, fill = black!10},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.907\\
0.910\\
0.869\\
0.848\\
0.905\\
0.904\\
0.889\\
0.889\\
0.891\\
0.836\\
0.820\\
0.868\\
0.868\\
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.895\\
0.887\\
0.809\\
0.809\\
0.847\\
0.848\\
0.844\\
0.854\\
0.863\\
0.775\\
0.761\\
0.789\\
0.883\\
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black, fill = black!10},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.878\\
0.878\\
0.838\\
0.817\\
0.884\\
0.893\\
0.879\\
0.877\\
0.877\\
0.817\\
0.810\\
0.830\\
0.936\\
};
\end{axis}
\end{tikzpicture}
\caption{Distribution of array performance ratios without and with temperature correction}
\label{fig:distribution pr}
\end{figure}
\end{document}
答案1
根据手册判断,pgfplots
不计算平均值。如果手动计算是一种选择,那么您可以使用散点图。添加
scatter/classes={
a={mark=*,red!50!black},
b={mark=*,blue!50!black},
c={mark=*,green!50!black},
d={mark=*,white!50!black}
}
到axis
选项,然后使用
\addplot [scatter,only marks,scatter src=explicit symbolic]
table [
x expr={1/3 + floor(\coordindex/2) + 1/3*mod(\coordindex,2)},
y=y,
meta=meta
]
{
y meta
0.86 a
0.89 a
0.86 b
0.89 b
0.86 c
0.89 c
0.82 d
0.85 d
};
当然,您必须修改 y 值。请注意,元列中的标签与散点设置中的类别相对应,这就是颜色的设置方式。
完整代码:
\documentclass[11pt, twoside, a4paper]{report}
\usepackage[inner = 30mm, outer = 20mm, top = 30mm, bottom = 20mm, headheight = 13.6pt]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
boxplot/draw direction=y,
ylabel={PR\textsubscript{A}},
axis lines*=left,
ymin = 0.75, ymax = 0.95,
ymajorgrids,
y tick label style={
/pgf/number format/.cd,
fixed,
fixed zerofill,
precision=2,
/tikz/.cd
},
height=8cm,
boxplot={
%
% Idea:
% place the
% group 1 at 0.3333 and 0.6666
% group 2 at 1.3333 and 1.6666
% group 3 at 2.3333 and 2.6666
% ...
% in a formular:
draw position={1/3 + floor(\plotnumofactualtype/2) + 1/3*mod(\plotnumofactualtype,2)},
%
% that means the box extend must be at most 0.33333 :
box extend=0.3,
},
% ... it also means that 1 unit in x controls the width:
x=3.2cm,
% ... and it means that we should describe intervals:
xtick={0,1,2,...,10},
x tick label as interval,
xticklabels={%
{System 1\\{\small without/with}},%
{System 2\\{\small without/with}},%
{System 3\\{\small without/with}},%
{System 4\\{\small without/with}},%
},
x tick label style={
text width=5cm,
align=center
},
scatter/classes={
a={mark=*,red!50!black},
b={mark=*,blue!50!black},
c={mark=*,green!50!black},
d={mark=*,white!50!black}
}
]
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.926\\
0.940\\
0.862\\
0.849\\
0.871\\
0.894\\
0.903\\
0.885\\
0.892\\
0.837\\
0.814\\
0.818\\
0.810\\
};
\addplot[boxplot={ every box/.style={very thick,draw=red!50!black, fill = black!10},
every whisker/.style={red!50!black,very thick},
every median/.style={red!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.944\\
0.892\\
0.857\\
0.904\\
0.938\\
0.930\\
0.907\\
0.910\\
0.874\\
0.857\\
0.854\\
0.853\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.923\\
0.921\\
0.857\\
0.867\\
0.886\\
0.880\\
0.886\\
0.888\\
0.893\\
0.826\\
0.818\\
0.857\\
0.848\\
};
\addplot[boxplot={ every box/.style={very thick,draw=blue!50!black, fill = black!10},
every whisker/.style={blue!50!black,very thick},
every median/.style={blue!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.913\\
0.916\\
0.881\\
0.869\\
0.917\\
0.919\\
0.909\\
0.905\\
0.907\\
0.859\\
0.858\\
0.892\\
0.891\\
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.915\\
0.912\\
0.840\\
0.845\\
0.873\\
0.868\\
0.875\\
0.877\\
0.883\\
0.812\\
0.789\\
0.840\\
0.833\\
};
\addplot[boxplot={ every box/.style={very thick,draw=green!50!black, fill = black!10},
every whisker/.style={green!50!black,very thick},
every median/.style={green!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.907\\
0.910\\
0.869\\
0.848\\
0.905\\
0.904\\
0.889\\
0.889\\
0.891\\
0.836\\
0.820\\
0.868\\
0.868\\
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.895\\
0.887\\
0.809\\
0.809\\
0.847\\
0.848\\
0.844\\
0.854\\
0.863\\
0.775\\
0.761\\
0.789\\
0.883\\
};
\addplot[boxplot={ every box/.style={very thick,draw=white!50!black, fill = black!10},
every whisker/.style={white!50!black,very thick},
every median/.style={white!50!black,very thick}}]
table[row sep=\\,y index=0] {
data\\
0.878\\
0.878\\
0.838\\
0.817\\
0.884\\
0.893\\
0.879\\
0.877\\
0.877\\
0.817\\
0.810\\
0.830\\
0.936\\
};
\addplot [scatter,only marks,scatter src=explicit symbolic]
table [
x expr={1/3 + floor(\coordindex/2) + 1/3*mod(\coordindex,2)},
y=y,
meta=meta
]
{
y meta
0.86 a
0.89 a
0.86 b
0.89 b
0.86 c
0.89 c
0.82 d
0.85 d
};
\end{axis}
\end{tikzpicture}
\caption{Distribution of array performance ratios without and with temperature correction}
\label{fig:distribution pr}
\end{figure}
\end{document}