在箱线图上将 x 轴数字更改为不同的值,

在箱线图上将 x 轴数字更改为不同的值,

我必须创建多个箱形图,如下所示,这是我从将数字添加到蛇形图(箱线图)

我想将轴的开始和结束值更改为不同的值,例如开始12完成23,这样我就可以将它们提供给我的学生,他们会给我五点总结。

我更改了值以获取最后一个刻度。我可以通过更改所有值来创建新的箱线图,但这非常耗时。

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}
\usetikzlibrary{snakes,arrows,backgrounds,decorations.pathreplacing}
    \begin{document}
    \begin{tikzpicture} [thick, framed]
        \filldraw[fill=green!20] (2.85,0) rectangle (5.7,1);
    \draw (4.45,0)--(4.45,1); 
    \draw (5.7,0.5)--(11,0.5);%vandret linie til max
    \draw (2.85,0.5)--(1,0.5);%vandret linie til min
    \draw (11,0.39)--(11,0.61);
    \draw (1,0.39)--(1,0.61);
    \draw [
    postaction={ 
        draw,
        decoration=ticks,
        segment length=1cm, % this changes number of ticks
        decorate,
    }
    ] (0,-1) -- (11.005,-1); % 11.005 need for last tick
    \foreach \tick in {0,...,11}
    \node at (\tick,-1) [below=1pt] {\tick};
         \end{tikzpicture}
    \end{document}

在此处输入图片描述

答案1

通过在循环中添加新计数器,\foreach您可以将刻度位置的计数器和刻度的值分开:

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}
\usetikzlibrary{snakes,arrows,backgrounds,decorations.pathreplacing}

\begin{document}
    \begin{tikzpicture} [thick, framed]
    \filldraw[fill=green!20] (2.85,0) rectangle (5.7,1);
\draw (4.45,0)--(4.45,1);
\draw (5.7,0.5)--(11,0.5);%vandret linie til max
\draw (2.85,0.5)--(1,0.5);%vandret linie til min
\draw (11,0.39)--(11,0.61);
\draw (1,0.39)--(1,0.61);
\draw [
postaction={
    draw,
    decoration=ticks,
    segment length=1cm, % this changes number of ticks
    decorate,
}
] (0,-1) -- (11.005,-1); % 11.005 need for last tick
\foreach \tick [count=\x from 0] in {12,...,23} % <----
\node at (\x,-1) [below=1pt] {\tick};           % <----  
     \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

另一个选择boxplotpgfplots

\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\usetikzlibrary{backgrounds}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[show background rectangle]
\begin{axis}[
  axis x line=bottom,
  axis x line shift=5mm,
  axis y line=none,
  y=1cm,
  xtick distance=1,
  xmin=-0.2, xmax=12.4,
]
\addplot+ [
  fill=green!20, draw=black,
  boxplot prepared={
  lower whisker=1, lower quartile=2.85,
  median=4.45,
  upper quartile=5.7, upper whisker=11,
},
  ] coordinates {};
\end{axis}
\end{tikzpicture}
\par
\vspace{1cm}
\begin{tikzpicture}[show background rectangle]
\begin{axis}[
  axis x line=bottom,
  axis x line shift=5mm,
  axis y line=none,
  y=1cm,
  xtick distance=1,
  xmin=11.8, xmax=24.4,
]
\addplot+ [
  fill=red!20, draw=black,
  boxplot prepared={
  lower whisker=13, lower quartile=14.85,
  median=16.45,
  upper quartile=17.7, upper whisker=23,
},
  ] coordinates {};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容