5

Good morning!

How can I put the yticklabels on the right side of yaxis? (Not in the right side of the picture, like here)

This is the code that I use:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-0.7,-0.5,...,0.1},
    ytick={0,5,...,50},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    width=0.8\textwidth,
    xmin=-0.8,
    xmax=0.2,
    ymin=-1,
    ymax=55]
    \end{axis}
    \end{tikzpicture}

\end{document}

and this is the (not wanted) result.

Thank's in advanced!!!

enter image description here

6

You can use

\pgfplotsset{
    every y tick label/.style={
        anchor=near yticklabel opposite,
        xshift=0.2em,
    }
}

to always have the y tick labels to the right of the axis:

enter image description here

If you don't to apply this globally you can apply that style of a per axis basis as per the MWE below.

Code:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}

    \begin{tikzpicture}[>=latex]
    \begin{axis}[
    grid,
    axis x line=center,
    axis y line=center,
    xtick={-0.7,-0.5,...,0.1},
    ytick={0,5,...,50},
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    width=0.8\textwidth,
    xmin=-0.8,
    xmax=0.2,
    ymin=-1,
    ymax=55,
    every y tick label/.style={
        anchor=near yticklabel opposite,
        xshift=0.2em,
    }
    ]
    \end{axis}
    \end{tikzpicture}

\end{document}
5

This is just a minor improvement of Peters answer. In his solution he shifted the labels "manually", but an "automatic" shift is possible.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.3}
\begin{document}
\begin{tikzpicture}[>=latex]
    \begin{axis}[
%        grid,                          % <-- commented so the result is clearer visible
        axis x line=center,
        axis y line=center,
        xtick={-0.7,-0.5,...,0.1},
        ytick={0,5,...,50},
        xlabel={$x$},
        ylabel={$y$},
        xlabel style={below right},
        ylabel style={above left},
        width=0.8\textwidth,
        xmin=-0.8,
        xmax=0.2,
        ymin=-1,
        ymax=55,
        major tick length=1cm,          % <-- for debugging purposes only
        every y tick label/.style={
            anchor=near yticklabel opposite,
            xshift=\pgfkeysvalueof{/pgfplots/major tick length},
        },
    ]
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

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.