我目前不知道如何为我的箱线图获取 x 轴断点。通过浏览稍微相关的 tex.stackexchange 帖子,我猜这应该可以通过 groupplot 实现,但是当我将代码从 axis 切换到 groupplot 时,我的箱线图不会显示。
为了说明我想要实现的目标:
对此有什么建议吗?
无 break 和 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 值。
始终将An
axis 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}