4

When I put bracket in a xlabel, the alignement of my figure change. It's just a little move, but it's uggly because i put two sub figures side by side.

Do you know how to solve this ?

Thanks in advance,

Thomas

PS : I think Here : TikZ graph not aligning is the same problem, but unresolved.

enter image description here

\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}
2
  • To x label of the first sub figure add \vphantom{)}.
    – Zarko
    Jan 7, 2015 at 1:10
  • it is a cheat! But it's work ! ^^ Thank you
    – Thomas
    Jan 7, 2015 at 2:19

2 Answers 2

4

There are several ways to fix this:

  1. As per Zarko's comment you can add a xlabel={$x$ \vphantom{(test)}}. The reason is the initial mismatch was the additional height of the (. Adding a \vphantom ensures that the both labels are of the same height. You actually on need a \vphantom{(}, but in other cases you may need the full text of the other label.

  2. Add a strut as in xlabel={$x$\strut}. The \strut should be added to both of the labels.

  3. Specify a style for the xlabel which positions the label correctly (as in the MWE below).

    enter image description here

Notes:

  • Axis labels should probably be in math mode as per the MWE below.

Code:

\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}
3
  • Thanks for this complete answer. I prefer the solution 2, it's easier.
    – Thomas
    Jan 7, 2015 at 4:32
  • 1
    In your xlabel style, anchor=base is preferable to anchor=center. Jan 7, 2015 at 10:32
  • @PaulGaborit: Thanks. I knew there was something better than center but couldn't remember at the time -- must be getting old. :-) Jan 7, 2015 at 20:14
1

A label generally fit on one line. So you can set the height and depth of the text for all axis labels:

\pgfplotsset{
  compat=1.11,
  label style={text height=.8em,text depth=.15em},
}

With this style, your figures are correctly aligned (horizontally and vertically) and labels use the same baseline.

Your MWE, completed:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.