[Tex/LaTex] pgfplots: Gap Between Axis and Axis Label With Extra Tick on Top: Remove How

axispgfplotsticks

Desired Outcome

I'm trying to create a plot with xticks and xtick labels on the lower x axis, along with an extra x tick on the upper x axis. The desired outcome is (almost) this:

desired outcome

MWE

\documentclass[margin=2mm,tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            extra x ticks={-0.0012},
            extra x tick labels={IMPORTANT},
            extra x tick style={
                grid=major,
                tick label style={
                    rotate=90,
                    anchor=west,
                },
                tick pos = right,
                ticklabel pos = right,
            },
            width=120mm,
            height=80mm,
            xlabel=Time,
            ylabel=Amplitude,
        ]
        \addplot+[
            ycomb,
            domain=-3e-3:3e-3,
            samples=11,
            every mark/.style={fill=white},
        ] 
        {exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
    \end{axis}
\end{tikzpicture}
\end{document}

Unfortunately, this leaves me with a rather unsightly gap between the x axis and its label (Time), as can be seen here:

current result

I have managed to find a workaround to get the desired result by manually positioning the x axis label, adding the following code:

xlabel style={at={(0.5,-2ex)}},

This results in the first picture I posted. However, this seems rather inelegant to me. For one thing, I haven't yet found out what the default vertical offset should be so that a plot created via this method has the same vertical offset for its x axis label as all other plots without manual intervention (so that all plots look identical in this aspect), and secondly, doing this manually just seems … wrong (yes, I know, sounds a bit silly).

Actual Question

So, my question boils down to these two points:

  • Is there a way to avoid this additional vertical gap and get pgfplots to do the "right" thing automatically, or
  • if I have to do it manually, what would the right (default) vertical offset be so that plots with automatic axis label placement and plots with manual placement have their axis label in the same location (I reckon this is documented somewhere, but I have yet to stumble across it in my search)?

Obviously, a proper solution for the first question would be preferable.

I have searched for various permutations of pgfplots, gap, axis label and so on, but so far no luck.

Best Answer

I can't think of a 100% automatic solution so far. The "problem" is, that for the position of the xlabel it is also accounted for the extra x ticks. Unfortunately it is not taken into account of the style modification ticklabel pos=right, so we have to undo this somehow.

I used the width and height functions of TikZ to shift the label accordingly, but of course have to give some values to the aforementioned functions, which are the "not 100% automatic" part of this solution. Of course in cases of more than one extra x ticks you have to give the widest text to the width function to make it work.

To prove that this gives the desired result I overlapped this solution with another one where I added the extra x tick labels by another method. As you can see there is only a very small shift remaining between the two solutions which I think is acceptable.

% used PGFPlots v1.14
\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
            extra x ticks={-0.0012},
            extra x tick labels={IMPORTANT},
            extra x tick style={
                grid=major,
                tick label style={
                    rotate=90,
                    anchor=west,
                },
                tick pos=right,
                ticklabel pos=right,
            },
            width=120mm,
            height=80mm,
            xlabel=Time,
            ylabel=Amplitude,
            % Because also the `extra x ticks' are taken into account for the
            % position of the `xlabel' (but not the fact that
            % `ticklabel pos=right' is used) this has to be "undone". To do so
            % we can use the `width' and `height' functions ...
            xlabel style={
                yshift = {width("IMPORTANT") - height("0") },
            },
        ]
        \addplot+[
            ycomb,
            domain=-3e-3:3e-3,
            samples=11,
            every mark/.style={fill=white},
        ] {exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};
    \end{axis}
    % -------------------------------------------------------------------------
    % for debugging purposes only
    % ---------------------------------
    % here I produce the desired result with a different approach to "know" where
    % the `xlabel' would be placed without the `extra x ticks'.
    \begin{axis}[
        extra x ticks={-0.0012},
        extra x tick labels={},
        extra x tick style={
            grid=major,
        },
        width=120mm,
        height=80mm,
        xlabel=Time,
        ylabel=Amplitude,
        xlabel style={
            red,
        },
        clip mode=individual,
    ]
        \addplot+[
            ycomb,
            domain=-3e-3:3e-3,
            samples=11,
            every mark/.style={fill=white},
        ] {exp(-x^2 / (2e-3^2)) / (1e-3 * sqrt(2*pi))};

        \node [rotate=90,anchor=west] at ( {axis description cs:0,1} -| -0.0012,0 ) {IMPORTANT};
    \end{axis}
    % -------------------------------------------------------------------------
\end{tikzpicture}
\end{document}

image showing the result of above code