4

I'd like to add some vertical space between the x tick labels and the x axis in a Pgfplots plot. Is this possible?

Background: I have two subfigures arranged next to each other (horizontally) using the subfig package. Both subfigures contain a Pgfplot. The left plot uses a logarithmic x axis and thus an exponential number format for the ticks. The right plot uses a linear scale x axis and thus ticks like 0, 100, 600, ... As a consequence, the left plot needs a tiny bit more of vertical space and is thus not aligned vertically with the right plot. My goal is to have the two plots vertically aligned by adding some \vphantom{} or the like. Here is an MWE producing the issue:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{subfig}
\begin{document}
\begin{figure}[h]
    \subfloat[Logarithmic horizontal axis]{
        \begin{tikzpicture}
        \begin{axis}[xmode=log, width=5cm, height=5cm]
        \addplot
        table[row sep=crcr]{%
        1     1\\
        1e6   2\\
        };
        \end{axis}
        \end{tikzpicture}%
    }%
    \subfloat[Linear horizontal axis]{
        \begin{tikzpicture}
        \begin{axis}[ytick pos=right, width=5cm, height=5cm]
        \addplot
        table[row sep=crcr]{%
        1     1\\
        10   2\\
        };
        \end{axis}
        \end{tikzpicture}%
    }%
\end{figure}
\end{document}

And this is a screenshot of the MWE output illustrating the vertical offset: MWE screenshot

  • 1
    Please give us the MWE which is needed to produce this. You could sett the ticks manually and give them a \vphatnom. I can't remember, if the ticks are parsed as math or not, but we can test that easily when we have some code. Thanks. – LaRiFaRi Jun 23 '15 at 8:29
  • @LaRiFaRi Added MWE – Deve Jun 24 '15 at 8:42
3

One solution is to specify a xticklabel style in order to shift it with yshift=-2pt, i.e.:

...
\subfloat[Linear horizontal axis]{
    \begin{tikzpicture}
    \begin{axis}[
ytick pos=right, width=5cm, height=5cm,
xticklabel style={yshift=-2pt},% <--- add in second subfigure
                ]
    \addplot
    table[row sep=crcr]{%
    1    1\\
    10   2\\
    };
    \end{axis}
    \end{tikzpicture}%
}%
...

enter image description here

0

As suggested by @LaRiFaRi the x tick labels can be defined programmatically using the Pgfplots key xticklabel. So adding the following option to the second axis does the trick:

xticklabel={\axisdefaultticklabel\vphantom{$10^6$}}

I have used the predefined command \axisdefaultticklabel which just prints the usual tick labels and added an extra space that corresponds to the height of the tick labels of the left, logarithmic axis. Here is the complete MWE for this solution

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepackage{subfig}
\begin{document}
\begin{figure}[h]
    \subfloat[Logarithmic horizontal axis]{
        \begin{tikzpicture}
        \begin{axis}[xmode=log, width=5cm, height=5cm]
        \addplot
        table[row sep=crcr]{%
        1     1\\
        1e6   2\\
        };
        \end{axis}
        \end{tikzpicture}%
    }%
    \subfloat[Linear horizontal axis]{
        \begin{tikzpicture}
        \begin{axis}[
        ytick pos=right, width=5cm, height=5cm, 
        xticklabel={\axisdefaultticklabel\vphantom{$10^6$}}
        ]
        \addplot
        table[row sep=crcr]{%
        1     1\\
        10   2\\
        };
        \end{axis}
        \end{tikzpicture}%
    }%
\end{figure}
\end{document}

And this is a screenshot of the result Screenshot of the ouput produced by the MWE

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.