pgfplots-带有彩色异常值的箱线图

pgfplots-带有彩色异常值的箱线图

我正在尝试按照以下建议的方式重新设计 pgfplots 箱线图丹尼尔·卡尔

  • 填充框而不是轮廓框
  • 加厚的胡须没有围栏
  • 所有元素(箱、须和异常值)的颜色取决于它们是高于还是低于中位数。

除了异常值的颜色外,我几乎已经完成了。以下是 MWE:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}
\pgfplotsset{
    every boxplot/.append style={
        scatter,
%        visualization depends on={((\thisrow{y}<\pgfplotsboxplotvalue{median}) ? "red" : "blue") \as \perpointcolor},
        % scatter/use mapped color={draw=\perpointcolor,fill=\perpointcolor},
        boxplot/draw/box/.code={
            \draw [/pgfplots/boxplot/every box/.try,
                   /pgfplots/boxplot/every lower box/.try]
                (boxplot box cs:\pgfplotsboxplotvalue{lower quartile},0)
                rectangle
                (boxplot box cs:\pgfplotsboxplotvalue{median},1); 
            \draw [/pgfplots/boxplot/every box/.try,
                   /pgfplots/boxplot/every upper box/.try]
                (boxplot box cs:\pgfplotsboxplotvalue{median},0)
                rectangle
                (boxplot box cs:\pgfplotsboxplotvalue{upper quartile},1); 
        },
        boxplot/draw/lower whisker/.code={
            \draw[/pgfplots/boxplot/every lower whisker/.try] (boxplot cs:\pgfplotsboxplotvalue{lower whisker})
              -- (boxplot cs:\pgfplotsboxplotvalue{lower quartile});
        },
        boxplot/draw/upper whisker/.code={
            \draw[/pgfplots/boxplot/every upper whisker/.try] (boxplot cs:\pgfplotsboxplotvalue{upper quartile})
              -- (boxplot cs:\pgfplotsboxplotvalue{upper whisker});
              }
        },       
    boxplot/.cd,
        every lower box/.style={draw=red,fill=red},
        every upper box/.style={draw=green,fill=green},
        every lower whisker/.style={ultra thick,draw=red},
        every upper whisker/.style={ultra thick,draw=green},
        every median/.style={black}
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        y=0.33cm,
        enlarge x limits,
        ytick=\empty,
        axis y line=none,
        axis equal image,
    ]
    \addplot+[
        boxplot prepared={
            lower whisker=42, lower quartile=45,
            median=47,
            upper quartile=47.5, upper whisker=48,
            sample size=1000,
        }] table [row sep=\\,y index=0] {40\\ 34\\ 56\\};
    \end{axis}
\end{tikzpicture}
\end{document}

sample output

这两行注释是我尝试根据离群值是低于还是高于中位数来为其着色。当我取消注释时,会出现错误Missing $ inserted

我不确定scatter密钥是否知道boxplot prepared密钥值。有什么想法吗?

答案1

当我意识到它point meta可以包含基于表数据的计算时,我明白了。它甚至知道键boxplot prepared

相关键为:

point meta=(x > \boxplotvalue{median}),
colormap={relativetomedian}{color=(red) color=(green)},
scatter/use mapped color={draw=mapped color,fill=mapped color},

第一行设置point meta01取决于绘制的 x 坐标是否小于或大于中位数。第二行设置两种颜色(中位数以下和以上)之间的颜色图。第三行设置点颜色。由于点元为 0 或 1,因此颜色是地图的最左侧或最右侧。

完整代码如下:

\documentclass[png]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}
\pgfplotsset{
    every boxplot/.append style={
        scatter,
        point meta=(x > \boxplotvalue{median}),
        colormap={relativetomedian}{color=(red) color=(green)},
        scatter/use mapped color={draw=mapped color,fill=mapped color},
        boxplot/draw/box/.code={
            \draw [/pgfplots/boxplot/every box/.try,
                   /pgfplots/boxplot/every lower box/.try]
                (boxplot box cs:\pgfplotsboxplotvalue{lower quartile},0)
                rectangle
                (boxplot box cs:\pgfplotsboxplotvalue{median},1); 
            \draw [/pgfplots/boxplot/every box/.try,
                   /pgfplots/boxplot/every upper box/.try]
                (boxplot box cs:\pgfplotsboxplotvalue{median},0)
                rectangle
                (boxplot box cs:\pgfplotsboxplotvalue{upper quartile},1); 
        },
        boxplot/draw/lower whisker/.code={
            \draw[/pgfplots/boxplot/every lower whisker/.try] (boxplot cs:\pgfplotsboxplotvalue{lower whisker})
              -- (boxplot cs:\pgfplotsboxplotvalue{lower quartile});
        },
        boxplot/draw/upper whisker/.code={
            \draw[/pgfplots/boxplot/every upper whisker/.try] (boxplot cs:\pgfplotsboxplotvalue{upper quartile})
              -- (boxplot cs:\pgfplotsboxplotvalue{upper whisker});
              }
        },       
    boxplot/.cd,
        every lower box/.style={draw=none,fill=red},
        every upper box/.style={draw=none,fill=green},
        every lower whisker/.style={ultra thick,draw=red},
        every upper whisker/.style={ultra thick,draw=green},
        every median/.style={black}
}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        y=0.33cm,
        enlarge x limits,
        ytick=\empty,
        axis y line=none,
        axis equal image,
    ]
    \addplot+[
        boxplot prepared={
            lower whisker=42, lower quartile=45,
            median=47,
            upper quartile=47.5, upper whisker=48,
            sample size=1000,
        }] table [row sep=\\,y index=0] {40\\ 34\\ 56\\};
    \end{axis}
\end{tikzpicture}
\end{document}

sample code output

相关内容