帶有群圖的箱線圖的 X 軸中斷

帶有群圖的箱線圖的 X 軸中斷

我目前對如何繼續為我的箱線圖獲取 x 軸中斷感到困惑。透過瀏覽稍微相關的 tex.stackexchange 帖子,我猜測這應該可以透過 groupplot 實現,但是當我將程式碼從 axis 切換到 groupplot 時,我的箱線圖不會顯示。

為了說明我想要實現的目標:

對此有什麼建議嗎?

沒有中斷和組圖的工作代碼:

\documentclass{minimal}  
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
ytick=\empty,
xmin=0, xmax=4500,
xtick={0,500,1000,2500,4500},
axis x line=bottom,
axis line style={-},
axis y line=none,
enlargelimits=0.05,
height=4.0cm, width=14.0cm,
]
\addplot+[
boxplot prepared={
upper quartile=650,
        lower quartile=30,
        upper whisker=1400,
        lower whisker=0,
        median=80
},black,
] coordinates {
(0,2100)
(0,2300)
(0,2900)
(0,3400)
(0,3700)
(0,4200)
};
\end{axis}
\end{tikzpicture}
\end{document}

帶有 x 軸中斷的箱線圖不起作用的程式碼:

\documentclass{minimal}  
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my fancy plots,
        group size=2 by 1,
        axis line style={-},
        ytick=\empty,
    },
    width=14cm,
]
\nextgroupplot[
               xtick={0,1000,2000,3000},
               axis x discontinuity=parallel,
               axis x line=bottom,
               width=10cm]
\addplot[
boxplot prepared={
upper quartile=650,
        lower quartile=30,
        upper whisker=1400,
        lower whisker=0,
        median=80
},black,
] coordinates {
(0,2100)
(0,2300)
(0,2900)
(0,3400)
(0,3700)
(0,4200)
};         
\nextgroupplot[xmin=9500,xmax=9750,
               xtick={9500,9750},
               axis x line=bottom,
               width=2.0cm]
\addplot coordinates {(0,9600)}; 
\end{groupplot}
\end{tikzpicture}
\end{document}

謝謝!

編輯:

將來參考的工作代碼:

\documentclass{minimal}  
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my fancy plots,
        group size=2 by 1,
        horizontal sep=3pt % added
    },
    % the following are added
    scale only axis,
    height=3cm,
    ymin=0.5,ymax=1.5,
    axis y line=none,
    axis x line=bottom,
]
\nextgroupplot[
               xmin=-100,
               ymin=0.5,ymax=1.5,
               xtick={0,1000,2000,3000},
               width=10cm,
               x axis line style={-{Bar[width=15pt]}}  
]
\addplot[
mark = x , mark options = {mark color=black},
boxplot prepared={
upper quartile=650,
        lower quartile=30,
        upper whisker=1400,
        lower whisker=0,
        median=80
},black,
] coordinates {
(0,2100)
(0,2300)
(0,2900)
(0,3400)
(0,3700)
(0,4200)
};

\nextgroupplot[xmin=9300,xmax=9900,
               xtick={9500,9750},
               width=2.0cm,
               x axis line style={{Bar[width=15pt]}-}
 ]
\addplot [
mark = x, mark options = {mark color=black}]
coordinates {(9600, 1)}; 
\end{groupplot}
\end{tikzpicture}
\end{document}

答案1

您的範例缺少的第一件事是\usepgfplotslibrary{groupplots, statistics},但我想您的實際文件中有這些。

您的程式碼中有一些錯誤。

  • 組圖中軸的常見選項不在 內部group style,而是在外部。因此,axis line style={-}, ytick=\empty需要移動。

  • 您只需設定軸的寬度,但如果不設定高度,則縱橫比將保持不變。另請考慮,預設情況下,軸的寬度和高度將設定為比指定長度小 45pt,以便為軸標籤騰出空間。添加scale only axis以使長度適用於軸盒本身,否則 2cm 寬的軸會變得相當小。

  • 在第二個軸的座標圖中,您交換了 x 值和 y 值。

  • Anaxis discontinuity始終放置在軸線的起點,但由於您已將其分成兩個軸,因此不需要它。相反,我使用例如更改了軸線的箭頭類型x axis line style={-|}來添加線條。 (或添加\usetikzlibrary{arrows.meta}並使用x axis line style={-{Bar[width=15pt]}}以獲得更長的條形。)

我想我還做了一些其他的調整,這些是我認為主要的事情。

在此輸入影像描述

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots, statistics}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
    group style={
        group name=my fancy plots,
        group size=2 by 1,
        horizontal sep=3pt % added
    },
    % the following are added
    scale only axis,
    height=3cm,
    ymin=0.5,ymax=1.5,
    axis y line=none,
    axis x line=bottom,
]
\nextgroupplot[
               xmin=-100,
               xtick={0,1000,2000,3000},
               width=10cm,
               x axis line style={-{Bar[width=15pt]}}  
]
\addplot[
boxplot prepared={
upper quartile=650,
        lower quartile=30,
        upper whisker=1400,
        lower whisker=0,
        median=80
},black,
] coordinates {
(0,2100)
(0,2300)
(0,2900)
(0,3400)
(0,3700)
(0,4200)
};

\nextgroupplot[xmin=9350,xmax=9900,
               xtick={9500,9750},
               width=2.0cm,
               x axis line style={{Bar[width=15pt]}-}
 ]
\addplot coordinates {(9600, 1)}; 
\end{groupplot}
\end{tikzpicture}
\end{document}

相關內容