我正在尝试绘制一系列箱线图并将它们包含/插入到表中。代码来自使用数据表定义一系列箱线图的帖子。表格包含的代码需要使用 将绘图定义为宏。看似简单,但事实并非如此。箱线图的代码使用带\newcommand
参数的宏,因此宏中还有宏。我第一次尝试用和替换,但这无法编译。为了使问题复杂化,我使用了图层(背景有 2 个彩色矩形,突出显示与每年箱线图形成对比的整体 IQR)。我的问题围绕一种更简单的绘制箱线图的方法,以及如何将主要绘图代码编写为可以插入的宏?我包含了绘制我需要插入表格的绘图的基本代码 MWE。任何见解都将不胜感激。#1
##1
##1
####1
\pgfplotstabletypeset
\documentclass{article}
\usepackage{booktabs,colortbl}
\usepackage{pgfplotstable,tikz}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=newest}
\pgfplotsset{every axis/.append style={thick},
every tick/.append style={thick}}
\pgfkeys{/pgfplots/x axis shift up/.style={
x axis line style={yshift=#1},
xtick style={yshift=#1},
xticklabel shift={#1}}}
\pgfkeys{/pgfplots/y axis shift left/.style={
y axis line style={xshift=-#1},
ytick style={xshift=-#1},
yticklabel shift={#1}}}
\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,
make style readable from table=average
}
\makeatother
\pgfplotstableread[row sep=crcr]{
yr lw lq med uq uw av\\
1997 2.90 8.08 9.45 11.35 15.60 9.750\\
1998 2.40 8.50 12.50 18.00 51.30 13.910\\
1999 6.00 8.85 11.60 19.50 30.00 13.795\\
2000 7.70 9.50 15.00 20.50 41.10 16.671\\
2001 6.50 14.00 16.00 20.78 41.10 18.061\\
2002 7.40 14.83 18.50 25.83 42.00 20.825\\
2003 11.50 16.68 18.13 19.88 31.20 19.446\\
2004 4.40 12.21 15.14 16.83 20.20 14.053\\
2005 6.00 8.85 11.10 13.30 22.00 11.914\\
2006 3.50 8.23 12.70 15.90 17.80 11.875\\
2007 6.30 9.40 12.70 12.70 17.50 11.720\\
2008 2.00 15.10 16.50 20.00 32.10 17.311\\
2009 7.00 18.80 23.60 29.17 53.72 25.151\\
2010 11.00 21.75 26.52 30.88 42.10 26.373\\
2011 10.30 21.73 26.90 31.83 45.30 27.117\\
2012 12.20 17.48 21.90 26.98 40.30 23.170\\
2013 10.20 17.75 21.60 24.80 30.50 21.352\\
2014 12.50 21.45 24.80 32.50 38.70 26.180\\
}\datatable
\edef\desmu{19.06}
\edef\dessd{8.89}
\edef\lci{18.28}
\edef\uci{19.84}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\edef\ciline{1}
\pgfplotstablegetrowsof{\datatable}
\edef\numberofticks{\pgfplotsretval}
\pgfmathsetmacro\numberofrows{\pgfplotsretval-1}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[small,
boxplot/draw direction=x, ymin=0, width=9cm,
height=9cm, clip=false,
ytick={1,...,\numberofticks},
xmajorgrids, major grid style=gray!20,
xtick={0,10,15,20,25,30,35,40,45,50,60},
yticklabels from table={\datatable}{[index]0},
xmin=0, xmax=60, axis x line*=top,
axis y line*=left, separate axis lines,
x axis shift up=5pt, y axis shift left=15pt,
xticklabel pos=upper, tick align=outside,
xticklabel style={major tick length=0pt,
thick, color=black,tickwidth={5pt}},
y axis line style={opacity=0}, y dir=reverse,
]
\pgfplotsinvokeforeach{0,...,\numberofrows}{
\addplot[
boxplot prepared from table={
table=\datatable,
row= #1, %row=\i,
lower whisker=lw,
upper whisker=uw,
lower quartile=lq,
upper quartile=uq,
average=av,%des
median=med
}, boxplot prepared,
box extend=0.5, % height of box
whisker extend=0.5, % height of whiskers
every box/.style={thin,draw=black,fill=gray!50},
every whisker/.style={black,thick},
every median/.style={black,thick},
every average/.style={draw=red, /tikz/mark=* },
/pgf/number format/precision=2
]
coordinates {}
node[left,black] at
(boxplot box cs: \boxplotvalue{lower whisker},0.5)
{\tiny\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
node[right,black] at
(boxplot box cs: \boxplotvalue{upper whisker},0.5)
{\tiny\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
} %foreach
\begin{pgfonlayer}{background}
\fill[color=blue!10] (axis cs:12.2,\numberofticks) rectangle
(axis cs:24.6,\ciline);
\fill[color=red!10] (axis cs:\lci,\numberofticks) rectangle
(axis cs:\uci,\ciline);
\draw [red,dotted,thick] (axis cs:\desmu,0.5) --
(axis cs:\desmu,\numberofticks)
node[anchor=south] at (axis cs:\desmu,0.5){\tiny $ \mu$}
node[blue,anchor=south east] at (axis cs:12.2,0.75)
{\tiny $Q_1$}
node[blue,anchor=south west] at (axis cs:24.6,0.75)
{\tiny $Q_3$};
\draw [blue,dotted,thick] (axis cs:17.8,0.5) --
(axis cs:17.8,\numberofticks)
node[blue,anchor=south east] at (axis cs:17.8,0.75)
{\tiny $Q_2$};
\end{pgfonlayer}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}