我创建了一些应该相互对齐的箱线图。我注意到,如果上部晶须为“5“。如果上部晶须为 4.99,则不会发生这种情况。有什么想法为什么会发生这种情况以及如何解决它?
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\one},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=50,
upper quartile=50,
lower quartile=49,
upper whisker=56,
lower whisker=40
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
% xmin = 2.5,
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\two},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=4,
upper quartile=4,
lower quartile=3,
upper whisker=5, %4.99 --> not moving to the left
lower whisker=3
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\three},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=2,
upper quartile=3,
lower quartile=2,
upper whisker=7,
lower whisker=1
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\caption{todo}
\end{figure}
\end{document}
// 编辑:此最小示例的一个解决方案是将最小值放入xmin = 2.5,
图的标题中。该xmax
值再次破坏了对齐。它对我的大型 tex 文档也有帮助,但仍然没有完美对齐。
答案1
嗯,问题在于“外部” xtick 5.2。您可以通过将样式设置为覆盖来抑制效果(但随后您必须在绘图下方添加空间),或者您可以明确设置 xtick:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\one},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=50,
upper quartile=50,
lower quartile=49,
upper whisker=56,
lower whisker=40
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
% xmin = 2.5,
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\two},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
%xticklabel style={overlay}, %also possible
xtick={3,3.2,...,5} %<------
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=4,
upper quartile=4,
lower quartile=3,
upper whisker=5, %4.99 --> not moving to the left
lower whisker=3
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}
[
ytick={1},
yticklabel style={text width=2.0cm, align=right},
yticklabels={plot\\three},
width=(\textwidth-1.0cm),
height=2.7cm,
ybar,
xmajorgrids=true,
grid style=dashed,
]
\addplot+[boxplot, draw=black,
boxplot prepared={
median=2,
upper quartile=3,
lower quartile=2,
upper whisker=7,
lower whisker=1
},
] coordinates {};
\end{axis}
\end{tikzpicture}
\caption{todo}
\end{figure}
\end{document}