如何用不同的预定义颜色填充每个 pgfplots 框?以下代码来自https://www.latex4technics.com/?note=7WU2A1。
\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{testdata.dat}
0 1 1.2 0.4 1.5 0.2
1 2 2.3 1.5 2.7 1
2 0.7 1.4 0.5 1.9 0.1
3 1 1.2 0.4 1.5 0.2
4 2 2.3 1.5 2.7 1
5 0.7 1.4 0.5 1.9 0.1
\end{filecontents}
\pgfplotsset{
box plot width/.initial=1em,
box plot/.style={
/pgfplots/.cd,
black,
only marks,
mark=-,
mark size=\pgfkeysvalueof{/pgfplots/box plot width},
/pgfplots/error bars/.cd,
y dir=plus,
y explicit,
},
box plot box/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\draw ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
},
/pgfplots/table/.cd,
y index=2,
y error expr={\thisrowno{3}-\thisrowno{2}},
/pgfplots/box plot
},
box plot top whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=4,
y error expr={\thisrowno{2}-\thisrowno{4}},
/pgfplots/box plot
},
box plot bottom whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=5,
y error expr={\thisrowno{3}-\thisrowno{5}},
/pgfplots/box plot
},
box plot median/.style={
/pgfplots/box plot
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [enlarge x limits=0.5,xtick=data, box plot width=0.5em]
\addplot [box plot median] table {testdata.dat};
\addplot [box plot box] table {testdata.dat};
\addplot [box plot top whisker] table {testdata.dat};
\addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
解决方案(即绘图)基于一个接受 7 个参数的pic
元素boxWW
;一行的六个数字和颜色。
数据是通过\csvreader
命令从外部文件读取的。因此,解决方案需要包csvimple
。当然,如果数据提供方式不同,代码也应该相应更改。
使用该命令filecontents
构建一个数据文件来获得一个示例(几乎就是问题中出现的示例)。
我也搞不清楚颜色是如何提供的;所以我即兴设计了一个基于每行数字的函数。它不是很有趣,但它是基于代码的rgb
;如果需要,你可以轻松地更改它。
代码
\documentclass[10pt, margin=17pt]{standalone}
\usepackage{csvsimple}
\usepackage{filecontents}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\begin{document}
\begin{filecontents}{zBoxes.dat}
0, 1, 1.2, 0.4, 1.5, 0.2
1, 2, 2.3, 1.5, 2.7, 1
2, 0.7, 1.4, 0.5, 1.9, 0.1
3, 1, 1.4, 0.4, 1.6, 0.2
4, 2, 2.3, 1.5, 2.7, 1
5, 0.7, 1.4, 0.5, 1.9, 0.1
\end{filecontents}
\tikzset{%
pics/boxWW/.style n args={7}{% ... / color
code={%
\path (0, #2) coordinate (M);
\path (0, #3) coordinate (T);
\path (0, #4) coordinate (B);
\draw[fill=#7] ($(B) +(-1ex, 0)$) rectangle ($(T) +(1ex, 0)$);
\draw (M) ++(-1ex, 0) -- ++(2ex, 0);
\draw (T) -- (0, #5) ++(-1ex, 0) -- ++(2ex, 0);
\draw (B) -- (0, #6) ++(-1ex, 0) -- ++(2ex, 0);
}
}
}
\begin{tikzpicture}[every node/.style={inner sep=0}]
% the axes
\draw (-1.1, 0) -- (6, 0);
\draw (-1, 0) -- ++(0, 3);
\foreach \i in {-1, ..., 5}{%
\draw[very thin] (\i, 0) -- ++(0, -3pt) node[pos=1.7, below] {\i};
}
\foreach \j in {0, 1, 2}{%
\draw[very thin] (-1, \j) ++(6pt, 0) -- ++(-8pt, 0) node[pos=1.3, left] {\j};
}
% boxes with whiskers
\csvreader[no head]{zBoxes.dat}{%
1=\bno, 2=\bmed, 3=\bmax, 4=\bmin, 5=\bwmax, 6=\bwmin}
{%
\tikzmath{% color constants
real \tmpr, \tmpg, \tmpb;
\tmpb = \bwmin/\bwmax;
\tmpg = \bmin/\bwmax;
\tmpr = \bmax/\bwmax;
}
\xdefinecolor{tmp}{rgb}{\tmpr,\tmpg,\tmpb}
\draw (\bno, 0) pic {boxWW={\bno}{\bmed}{\bmax}{\bmin}{\bwmax}{\bwmin}{tmp}};
}
\end{tikzpicture}
\end{document}