2

I try to specify the x axis label position below the x axis arrow at the same baseline like the xticklabels. It works fine for most plots. However, when modifying the range of the y axis with ymin=..., the coordinate of current axis.south east changes (which I don't like), but in addition, the position of the coordinate changes towards an unexpected direction.

What is the reason that truncated plots result in a strangely modified current axis environment?

In the following, pictures with ymin=1.0 (ok), ymin=0.8 (ok), ymin=1.2 (undesirable result), and ymin not defined (strange result) are provided, together with an MWE.

ymin=1.0, result ok ymin=0.8, result ok ymin=1.2, result undesirable no ymin, result strange

MWE:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.13}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
        axis x line=bottom,
        every axis x label/.style={at={($ (current axis.south east) + (0,-2.135pt)$)},red,anchor=base,yshift=-0.75em},
        xticklabel style={anchor=base,yshift=-0.75em},
        xlabel={$t$}, 
        ymin=1.0, % <-- min of addplot
        %ymin=1.2, % <-- random truncation
    ]
    \addplot[black] coordinates {(0,2) (1,1) (2.2,2)};

\end{axis}
\end{tikzpicture}
\end{document}
kromuchi
  • 2,187
  • With alternate solutions provided below, I am still interested what causes a weird positioning using current axis.south east... Is this a bug in pgfplots or have I understood the meaning of current axis in a wrong way? – kromuchi Apr 09 '16 at 16:17

1 Answers1

3

You can use the xticklabel cs to position the x axis label:

every axis x label/.style={at={(xticklabel cs:1)},anchor=south,red},
typeset ticklabels with strut,
xlabel={$t$\strut},

The \strut inside xlabel and option typeset ticklabels with strut are used to ensure that the ticklabels and the label have the same height and the same depth.

enter image description here

Code:

\documentclass[tikz,margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\begin{document}
\foreach \ymin in {.8,1,1.2}{
\begin{tikzpicture}
\begin{axis}[
        axis x line=bottom,
        every axis x label/.style={at={(xticklabel cs:1)},anchor=south,red},
        typeset ticklabels with strut,
        xlabel={$t$\strut}, 
        ymin=\ymin
    ]
    \addplot[black] coordinates {(0,2) (1,1) (2.2,2)};
\end{axis}
\end{tikzpicture}}
\end{document}

Update:

If you really want to manually shift the x axis label by absolute values like in your example use either

at={([yshift=-2.135pt]current axis.south east)}

or

at={($ (current axis.south east) + (0pt,-2.135pt)$)}

Note, that you have to use 0pt instead only 0.

enter image description here

Code:

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usetikzlibrary{calc}
\begin{document}
\foreach \ymin in {.8,1,1.2}{
\begin{tikzpicture}
\begin{axis}[
        axis x line=bottom,
        every axis x label/.style={
            at={($ (current axis.south east) + (0pt,-2.135pt)$)},
            red,anchor=base,yshift=-0.75em
        },
        xticklabel style={anchor=base,yshift=-0.75em},
        xlabel={$tg$}, 
        ymin=\ymin
    ]
    \addplot[black] coordinates {(0,2) (1,1) (2.2,2)};
\end{axis}
\end{tikzpicture}}
\end{document}
esdd
  • 85,675
  • Thanks, your solution works. However, I would also like to understand what causes my MWE to crash. Is this a bug in pgfplots or have I understood the meaning of current axis in a wrong way? – kromuchi Apr 09 '16 at 16:15
  • 1
    @kromuchi See my updated answer. You have to use 0pt instead only 0 for the x direction. – esdd Apr 09 '16 at 17:23
  • Crazy, that (wrong) units for x coordinates effect the y position. – kromuchi Apr 09 '16 at 17:30
  • For further reference: There is also a similar coordinate system to xticklabel cs that ignores the height of x labels: xticklabel* cs – kromuchi Apr 09 '16 at 17:34