当我在 xlabel 中放置括号时,我的图形的对齐方式会发生变化。这只是一个小小的移动,但它很丑陋,因为我将两个子图形并排放置。
你知道如何解决这个问题吗?
提前致谢,
托马斯
附言:我认为这里 :TikZ 图表未对齐是同样的问题,但尚未解决。
\documentclass[10pt]{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 1}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={x},
scaled x ticks = false,
ylabel={y},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
~
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 2}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={x {(test)}},
scaled x ticks = false,
ylabel={x},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\vfill
\end{document}
答案1
有几种方法可以解决这个问题:
根据 Zarko 的评论,您可以添加一个
xlabel={$x$ \vphantom{(test)}}
。原因是最初的不匹配是 的额外高度(
。添加\vphantom
可确保两个标签的高度相同。您实际上只需要一个\vphantom{(}
,但在其他情况下,您可能需要另一个标签的全文。添加一个
strut
,如xlabel={$x$\strut}
。\strut
应该添加到两个都标签。指定一种样式,以便
xlabel
正确定位标签(如下面的 MWE 所示)。
笔记:
- 轴标签可能应该按照下面的 MWE 处于数学模式。
代码:
\documentclass[10pt]{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 1}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={$x$},
scaled x ticks = false,
ylabel={$y$},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
xlabel style={at={(0.5,0)}, below,yshift=-25pt, anchor=base, inner sep=0pt, minimum height=2.0ex}
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
~
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 2}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={$x$ (test)},
scaled x ticks = false,
ylabel={$x$},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
xlabel style={at={(0.5,0)}, below,yshift=-25pt, anchor=base, inner sep=0pt, minimum height=2.0ex}
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\vfill
\end{document}
答案2
标签通常占一行。因此,您可以设置所有轴标签的文本高度和深度:
\pgfplotsset{
compat=1.11,
label style={text height=.8em,text depth=.15em},
}
使用这种样式,您的图形可以正确对齐(水平和垂直),并且标签使用相同的baseline
。
您的 MWE,已完成:
\documentclass[10pt]{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\pgfplotsset{
compat=1.11,
label style={text height=.8em,text depth=.15em},
}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 1}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={x},
scaled x ticks = false,
ylabel={y},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
~
\begin{subfigure}[b]{0.45 \textwidth}
\caption{sub-figure 2}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\small]
\begin{axis}[height=\textwidth,width=\textwidth,
axis lines=left, enlarge x limits=true, enlarge y limits=true,
xlabel={x {(test)}},
scaled x ticks = false,
ylabel={x},
x tick label style={/pgf/number format/fixed, /pgf/number format/1000 sep = \thinspace},
]
\addplot[ybar,ybar interval=0] coordinates{
(0,8) (500,87) (1000,127) (1500,93) (2000,68) (2500,51) (3000,40) (3500,17) (4000,19) (4500,14) (5000,11) (5500,2) (6000,1) (6500,1) (7000,0)};
\end{axis}
\end{tikzpicture}
\end{subfigure}
\end{figure}
\vfill
\end{document}