# Plot changing abruptly when xmin is changed slightly

When I run the following

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{fillbetween}
\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-1.6,xmax=3,ymin=-1.5,ymax=1.5,axis x line=center,axis y line=center,xlabel=$x$,ylabel=$y$,  xlabel style=right, ylabel style=above]

\addplot [gray!50] fill between [of=xaxis and ymax,soft clip={domain=\pgfkeysvalueof{/pgfplots/xmin}:0}];
\addplot [gray!50] fill between [of=pip1 and ymax,soft clip={domain=0:\pgfkeysvalueof{/pgfplots/xmax}}];
\addplot [gray!50] fill between [of=xaxis and pip0,soft clip={domain=0:\pgfkeysvalueof{/pgfplots/xmax}}];
\addplot [gray!50] fill between [of=pim1 and pim0,soft clip={domain=0:\pgfkeysvalueof{/pgfplots/xmax}}];
\end{axis}
\end{tikzpicture}

\end{document}


I get a plot that looks as follows However, changing xmin to -2, say, causes all the shaded regions to turn white, except the part in the second quadrant. I'd like to know what am I doing wrong here? Why does changing the dimensions of the plot affect it so much?

Thank you!

In instances where paths overlap, such as at (0,0), defining the clip path using the syntax domain={} tends to have problems. A workaround is to specify a new path to set the clip path domain.

In this case, you can achieve what you need with two rectangles. The first defines the path encompassing the domain defined by the coordinates (xmin,0) (0,ymax). This is defined by a \newcommand called \clippatha. The second path is set the same way and is called \clippathb. The syntax were also simplified by defining some macros for key parameters such and xmin, xmax, ymin and ymax.

The fill between is then achieved using \addplot commands such as:

\addplot [blue!50] fill between [of=xaxis and ymax,soft clip= {\clippatha}];

After adding the suggested way of specifying the domains, we get the following plot at xmin=-2.0. Indeed, the plot is now insensitive for any xmin value.

This is the MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
\begin{document}

\pgfmathsetmacro\xmin{-2.0}
\pgfmathsetmacro\xmax{3.0}
\pgfmathsetmacro\ymin{-1.5}
\pgfmathsetmacro\ymax{1.5}

\newcommand\clippatha{(\xmin,0) rectangle (0,\ymax)}
\newcommand\clippathb{(0,0) rectangle (\xmax,\ymax)}

\begin{tikzpicture}
\begin{axis}[
xmin=\xmin, xmax=\xmax, ymin=\ymin, ymax=\ymax,
axis x line=center,axis y line=center,
xlabel=$x$,ylabel=$y$,
xlabel style=right, ylabel style=above,
axis on top
]
\path [name path=xaxis]  (\xmin,0)     --  (\xmax,0);
\path [name path=ymax]   (\xmin,\ymax) --  (\xmax,\ymax);