4

I want to right align the ticklabels in secondary y-axiz in pgfplot. Here is my output and MWE. Thanks in advance for your help.

enter image description here

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

\begin{document}

\begin{tikzpicture}

    \pgfplotsset{
        set layers,% --- CF
        , x tick label style={
             /pgf/number format/.cd,
             fixed,
             fixed zerofill,
             precision=1,
             /tikz/.cd
            }
          , y tick label style={
             /pgf/number format/.cd,
             fixed,
             fixed zerofill,
             precision=1,
             /tikz/.cd
            },
    }

    \begin{axis}[
        %   title = {$x \exp(-x^2-y^2)$}
        , xlabel = $x$
            , ylabel = $y$
            , domain = -1:1
            , y domain = -1:1
        , enlargelimits
        , view = {0}{90}
        , extra description/.code={% --- CF
                \xdef\XMIN{\pgfkeysvalueof{/pgfplots/xmin}}
                \xdef\XMAX{\pgfkeysvalueof{/pgfplots/xmax}}
                \xdef\YMIN{\pgfkeysvalueof{/pgfplots/ymin}}
                \xdef\YMAX{\pgfkeysvalueof{/pgfplots/ymax}}
        },
        ]

       \addplot3[
            contour gnuplot={
                  number = 10
                 },
             thick
            ]
         {
                    776.062 -50.812* x + 153.062 * y -76.812 *x *y
                    };

    \end{axis}

    \begin{axis}[% --- CF
        xmin=\XMIN,
        xmax=\XMAX,
        ymin=\YMIN,
        ymax=\YMAX,
        ticklabel pos=right,
    ]
    \end{axis}

\end{tikzpicture}

\end{document}
2

Another way, using yticklabel style={anchor=east,xshift=2.75em}. Unfortunately, it still requires manual adjustment (the xshift setting).

I've simplified your MWE significantly so the changes required are not drowned out by other style adjustments. (Also, I don't have gnuplot on this machine.)

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

\begin{document}
\begin{tikzpicture}
\pgfplotsset{
  set layers,
  domain=-1:1,
  tick label style={
    /pgf/number format/.cd,
    fixed, fixed zerofill,
    precision=1,
    /tikz/.cd,
  },
}
\begin{axis}[xlabel=$x$,ylabel=$y$]
  \addplot[thick] {x^3};
\end{axis}
\begin{axis}[
  ticklabel pos=right,
  yticklabel style={anchor=east,xshift=2.75em},
]
  \addplot {-x^3};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
2

From the link given by @Torbjørn, using yticklabel style={draw,text width=width("$-2.0$"),align=right} does the trick.

enter image description here

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

\begin{document}

\begin{tikzpicture}

    \pgfplotsset{
        set layers,% --- CF
        , x tick label style={
             /pgf/number format/.cd,
             fixed,
             fixed zerofill,
             precision=1,
             /tikz/.cd
            }
          , y tick label style={
             /pgf/number format/.cd,
             fixed,
             fixed zerofill,
             precision=1,
             /tikz/.cd
            },
    }

    \begin{axis}[
        %   title = {$x \exp(-x^2-y^2)$}
        , xlabel = $x$
            , ylabel = $y$
            , domain = -1:1
            , y domain = -1:1
        , enlargelimits
        , view = {0}{90}
        , extra description/.code={% --- CF
                \xdef\XMIN{\pgfkeysvalueof{/pgfplots/xmin}}
                \xdef\XMAX{\pgfkeysvalueof{/pgfplots/xmax}}
                \xdef\YMIN{\pgfkeysvalueof{/pgfplots/ymin}}
                \xdef\YMAX{\pgfkeysvalueof{/pgfplots/ymax}}
        },
        ]

       \addplot3[
            contour gnuplot={
                  number = 10
                 },
             thick
            ]
         {
                    776.062 -50.812* x + 153.062 * y -76.812 *x *y
                    };

    \end{axis}

    \begin{axis}[% --- CF
        xmin=\XMIN,
        xmax=\XMAX,
        ymin=\YMIN,
        ymax=\YMAX,
        ticklabel pos=right,
                yticklabel style={text width=width("$-2.0$"),align=right}
    ]
    \end{axis}

\end{tikzpicture}

\end{document}
| improve this answer | |
  • 1
    It is still slightly off. I think \widthof{$-2.0$} works better. – jens_bo Aug 8 '14 at 15:52
  • You right @jenson_bo. – MYaseen208 Aug 8 '14 at 16:00

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.