我可以在箱线图上画椭圆吗?

我可以在箱线图上画椭圆吗?

我在这里看到了一种制作箱线图的好方法。但我想标记特定学生的位置。所以我可以在箱线图上画一个小椭圆吗?例如在 82% 处?

我认为椭圆更好,因为它不会遮挡箱线图的视图。但是箱线图下方的较宽线条也是完美的。

    \begin{tikzpicture}
  \begin{axis}
    [cycle list name=mark list,
    width=12cm, height=3.5cm, 
    ytick={1,2,3,4,5,6,7,8},
    yticklabels={Samlet set},
    ]
% Alles data
    \addplot+[black,
    boxplot prepared={
      median=65,
      upper quartile=75.5,
      lower quartile=41,
      upper whisker=97.5,
      lower whisker=12
    },
    ] coordinates {};
  \end{axis}
\end{tikzpicture}

答案1

这将创建一个宽度为 2 个百分点、高度与箱线图大致相同的填充椭圆。关键是在轴环境中定义坐标,然后进行绘制。

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary[statistics]

\newlength{\xscale}
\newlength{\yscale}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [cycle list name=mark list,
    width=12cm, height=3.5cm, 
    ytick={1},
    yticklabels={Samlet set},
    ]
    \coordinate (start) at (axis cs: 0,0);% to compute x and y scales
    \coordinate (end) at (axis cs: 1,1);
% Alles data
    \addplot+[boxplot prepared={
      median=65,
      upper quartile=75.5,
      lower quartile=41,
      upper whisker=97.5,
      lower whisker=12
    },
    ] coordinates{};
    \coordinate (A) at (axis cs: 82,1);
  \end{axis}
  \pgfextractx{\xscale}{\pgfpointdiff{\pgfpointanchor{start}{center}}{\pgfpointanchor{end}{center}}}%
  \pgfextracty{\yscale}{\pgfpointdiff{\pgfpointanchor{start}{center}}{\pgfpointanchor{end}{center}}}%
  \fill (A) ellipse[x radius=\xscale,y radius=0.4\yscale];
\end{tikzpicture}

\end{document} 

演示


这是使用 实现的同样的事情\pgfplotsextra

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary[statistics]

\newlength{\xscale}
\newlength{\yscale}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [cycle list name=mark list,
    width=12cm, height=3.5cm, 
    ytick={1},
    yticklabels={Samlet set},
    ]
  \pgfplotsextra{% compute x and y scales
      \coordinate (start) at (axis cs: 0,0);
      \coordinate (end) at (axis cs: 1,1);
      \pgfextractx{\xscale}{\pgfpointdiff{\pgfpointanchor{start}{center}}{\pgfpointanchor{end}{center}}}%
      \pgfextracty{\yscale}{\pgfpointdiff{\pgfpointanchor{start}{center}}{\pgfpointanchor{end}{center}}}%
    }%
% Alles data
    \addplot+[boxplot prepared={
      median=65,
      upper quartile=75.5,
      lower quartile=41,
      upper whisker=97.5,
      lower whisker=12
    },
    ] coordinates{};
    \pgfplotsextra{\fill (axis cs: 82,1) ellipse[x radius=\xscale,y radius=0.4\yscale];}%
  \end{axis}

\end{tikzpicture}

\end{document} 

相关内容