1

When I have a y label on a plot, and I add an extra tick with a long description on the right, the y label gets shifted to the left. This seems to be a layout bug in the pgfplots code. What is the best way of resolving this? Note that before version 1.16 this did not happen.

Minimal Example:

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    extra y ticks = {.5},
    extra y tick labels = {very long desciption},
    extra y tick style  = {ticklabel pos=right},
    ylabel = {y axis label},
    ymin=0, ymax=1, xmin=0, xmax=1,
    ]
 \end{axis}
\end{tikzpicture}

\end{document}

Example plot

2

I tested back to v1.6 and I always get the same result as in your question. And this could be stated as a bug, but usually one writes the extra tick labels on the same "side" as the normal tick labels. So there currently doesn't exist a handler for this very special case ... (There are also a lot of other possible combinations of options, but it would be very hard to handle all of them "as expected" and also would make the compilation slow because of checking all these combinations.

Please find below the code that gives you the desired result.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        extra y ticks={0.5},
        % put the tick in a `\makebox` thus it does not consume any width
        % and the `ylabel` is positioned right
        extra y tick labels={\makebox[0pt][l]{very long description}},
        extra y tick style={
            ticklabel pos=right,
            % -----------------------------------
            % (needed to adjust the bounding box)
            yticklabel style={
                % add a name to the extra y tick label
                name=ylabel-\ticknum,
            },
            % -----------------------------------
        },
        ylabel={y axis label},
        ymin=0, ymax=1, xmin=0, xmax=1,
    ]
    \end{axis}
        % but then the bounding box doesn't account for the `extra y tick`.
        % Thus, determine the width of it ...
        \pgfmathsetlengthmacro{\MyExtraLabel}{width("very long description")}
        % ... and add that by an (invisible) path
        \path (ylabel-0) -- +(\MyExtraLabel,0);
\end{tikzpicture}
\end{document}

image showing the result of above code

  • Thanks for your answer. When you test on version 1.1 it works as expected. So something does seem to have regressed. However, thank you for your solution. – Lasse May 13 '19 at 20:50
  • Ok, I didn't write everything. I also tested with v1.3.1, but there moving the extra ticks to the right side didn't work. Thus, I didn't test any further. (Also from my point of view everything before v1.3 was not that good and one should avoid using a compat level below 1.3.) – Stefan Pinnow May 13 '19 at 20:57

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.