5

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 [draw=none,name path=xaxis,domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}] {0};
 \addplot [draw=none,name path=ymax,domain=\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}] {\pgfkeysvalueof{/pgfplots/ymax}};
 \addplot [name path=pip1,domain=0:\pgfkeysvalueof{/pgfplots/xmax},smooth,samples=500] {sqrt(x/2)};
 \addplot [name path=pip0,domain=0:\pgfkeysvalueof{/pgfplots/xmax},smooth,samples=500] {sqrt(x/6)};
 \addplot [name path=pim1,domain=0:\pgfkeysvalueof{/pgfplots/xmax},smooth,samples=500] {-sqrt(x/2)};
 \addplot [name path=pim0,domain=0:\pgfkeysvalueof{/pgfplots/xmax},smooth,samples=500] {-sqrt(x/6)};

 \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 enter image description here 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!

5

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.

enter image description here

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);

 \addplot [name path=pip1,domain={0:\xmax},smooth,samples=100] {sqrt(x/2)};
 \addplot [name path=pip0,domain={0:\xmax},smooth,samples=100] {sqrt(x/6)};
 \addplot [name path=pim1,domain={0:\xmax},smooth,samples=100] {-sqrt(x/2)};
 \addplot [name path=pim0,domain={0:\xmax},smooth,samples=100] {-sqrt(x/6)};

 \addplot [blue!50] fill between [of=xaxis and ymax,soft clip= {\clippatha}];
 \addplot [red!50] fill between  [of=pip1 and ymax,soft clip=  {\clippathb}];
 \addplot [gray!50] fill between [of=xaxis and pip0,soft clip= {\clippathb}];
 \addplot [cyan!50] fill between [of=pim1 and pim0,soft clip=  {\clippathb}];
 \end{axis}
 \end{tikzpicture}

 \end{document}

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.