pgfplots icon indicating copy to clipboard operation
pgfplots copied to clipboard

Strange interaction between "fill between", "transform shape" and label positioning.

Open Rmano opened this issue 5 years ago • 3 comments

Consider that code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[
                scale=0.6, transform shape
                ]
                \begin{axis}[
                    width=6cm, height=6cm,
                    xmin=350, xmax=1100,
                    domain=300:1000, samples=100,
                    ymin=0, ymax=1,
                    axis x line = center,
                    axis y line = center,
                    enlarge x limits,
                    enlarge y limits,
                    xlabel = {$\lambda$~(\si{\nm})},
                    every axis x label/.append style = {at={(1.0, -0.2)}, },
                    ylabel = {$\xi_r$},
                    legend style = {nodes={right, font=\scriptsize}},
                    legend pos = north east,
                    clip mode = individual,
                    ]
                    \addplot [ultra thick, green!50!black, name path=A, domain=450:800] {exp(-(x-600)/400*(x-600)/100)};
                    \path [name path=B] (450,0) -- (800,0);
                    % \addplot [green] fill between [of=A and B];
            \end{axis}
\end{tikzpicture}
\end{document}

it gives the expected:

image

(Look at the x label position). If I uncomment the last line in the axis:

\addplot [green] fill between [of=A and B];

then the output is:

image

Notice how the labels moves.

This does not happen if I comment the scale=0.6, transform shape option.

Rmano avatar Oct 12 '20 12:10 Rmano

Probably, coordinate parser is not taking scaling into account. Use explicitly relative axis coordinate system

every axis x label/.append style = {at={(rel axis cs:1.0, -0.2)}, },

ilayn avatar Oct 12 '20 13:10 ilayn

Ah, ok. That way it works. rel axis cs: is supposed to be the default; it seems that fill between somehow changes this.

Thanks for the workaround!

Rmano avatar Oct 12 '20 16:10 Rmano

I agree with @ilayn. However, one lesson I learnt is to never add transformations to the ambient tikzpicture. Much worse problems can arise. If you add scale=0.6 to the options of the axis, the problem disappears. One seems to need to transform the nodes separately, though.

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}\pgfplotsset{compat=1.17}
\usepgfplotslibrary{fillbetween}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
                \begin{axis}[scale=0.6,
					nodes={scale=0.6},
                    width=6cm, height=6cm,
                    xmin=350, xmax=1100,
                    domain=300:1000, samples=100,
                    ymin=0, ymax=1,
                    axis x line = center,
                    axis y line = center,
                    enlarge x limits,
                    enlarge y limits,
                    xlabel = {$\lambda$~(\si{\nm})},
                    every axis x label/.append style = {at={(1.0, -0.2)}, },
                    ylabel = {$\xi_r$},
                    legend style = {nodes={right, font=\scriptsize}},
                    legend pos = north east,
                    clip mode = individual,
                    ]
                    \addplot [ultra thick, green!50!black, name path=A, domain=450:800] {exp(-(x-600)/400*(x-600)/100)};
                    \path [name path=B] (450,0) -- (800,0);
                    \addplot [green] fill between [of=A and B];
            \end{axis}
\end{tikzpicture}
\end{document}
Screen Shot 2020-10-12 at 8 11 20 PM

marmotghost avatar Oct 13 '20 03:10 marmotghost