8

I would like to plot a function in the range [-2, 2], but PGFplots keeps plotting a little bit more:

enter image description here

Why is that the case and how do I fix it?

Code

\documentclass[varwidth=true, border=2pt]{standalone}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepackage{tikz}

\begin{document}
\pgfplotsset{
    colormap={whitered}{
        color(0.000cm)=(blue!50!black);
        color(0.125cm)=(blue);
        color(0.250cm)=(SkyBlue);
        color(0.375cm)=(SkyBlue);
        color(0.500cm)=(SpringGreen);
        color(0.625cm)=(yellow);
        color(0.750cm)=(yellow);
        color(0.875cm)=(red);
        color(1.000cm)=(red!50!black)
    }
}
\begin{tikzpicture}
    \begin{axis}[
        domain=-2:2,
        view={0}{90},
        axis background/.style={fill=white},
    ]
        \addplot3[surf,
                  samples=50,
                  shader=interp,
                  colormap name=whitered]
                {x/exp(x^2+y^2)};
        % \addplot3[contour gnuplot={number=15, labels=false},
        %           very thick,
        %           samples=30] 
        %         {x/exp(x^2+y^2)};
        \addplot3[blue,
                  quiver={
                          u={(1-2*x^2)*exp(-x^2-y^2)},
                          v={-2*x*y*exp(-x^2-y^2)},
                          scale arrows=0.3,
                         },
                  -stealth,samples=15]
                {x/exp(x^2+y^2)};
    \end{axis}
\end{tikzpicture}
\end{document}
10

This happens because the lengths of the quiver arrows are taken into account when calculating the axis range. The arrow that starts at (-2,0) has a non-zero length, the tip lies slightly to the left, so the axis range is increased so the full arrow is visible.

To limit the axis range to the function domain, set xmin=-2, xmax=2, ymin=-2, ymax=2.

I would also recommend setting axis equal image to get the same scaling along the x and y axis, and using the approach from Quiver plot with pgfplots: scale arrowheads with arrowsize for also scaling the arrowhead sizes, not just the lengths:

\documentclass[varwidth=true, border=2pt]{standalone}

\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{arrows.meta}

\begin{document}
\pgfplotsset{
    colormap={whitered}{
        color(0.000cm)=(blue!50!black);
        color(0.125cm)=(blue);
        color(0.250cm)=(SkyBlue);
        color(0.375cm)=(SkyBlue);
        color(0.500cm)=(SpringGreen);
        color(0.625cm)=(yellow);
        color(0.750cm)=(yellow);
        color(0.875cm)=(red);
        color(1.000cm)=(red!50!black)
    }
}
\begin{tikzpicture}
    \begin{axis}[
        domain=-2:2,
        view={0}{90},
        axis background/.style={fill=white},
        xmin=-2, xmax=2,
        ymin=-2, ymax=2,
        axis equal image,
        point meta rel=per plot
    ]
        \addplot3[surf,
                  samples=15,
                  shader=interp,
                  colormap name=whitered]
                {x/exp(x^2+y^2)};
        \addplot3[blue,
        point meta={
            sqrt(
                ((1-2*x^2)*exp(-x^2-y^2))^2+
                (-2*x*y*exp(-x^2-y^2))^2
            )        
        },
                  quiver={
                          u={(1-2*x^2)*exp(-x^2-y^2)},
                          v={-2*x*y*exp(-x^2-y^2)},
                          scale arrows=0.3,
                          every arrow/.append style={%
                             -{Latex[scale length={max(0.01,\pgfplotspointmetatransformed/1000)}]},
                             },
                         },
                         samples=15]
                {x/exp(x^2+y^2)};
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

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.