3

I'm trying to shade the domain bounded by the parabolas y=-x^2, y=x^2+1, and the lines x=-1, x=3.

By using the answer of @Salim Bou here pgfplots fillbetween with multiple curves, I've used the following code:

\begin{figure}[h]
  \begin{tikzpicture}
   \begin{axis}
     [xlabel=$x$,ylabel=$y$,axis lines=middle,xtick={30},ytick={30},no marks,axis equal,xmin=-2,xmax=4,ymin=-10,ymax=10,enlargelimits={upper=0.1}]

     \addplot[no markers,thick,samples=1001, samples y=0,domain=-1:3,variable=x, name path=A]({ x },{ -x^2 });
     \addplot[no markers,thick,samples=1001, samples y=0,domain=-1:3,variable=x, name path=B]({ x },{ x^2+1 });

     \addplot[no markers,thick,samples=3, samples y=0,domain=-1:2,variable=t]({ -1 },{ t });
     \addplot[no markers,thick,samples=3, samples y=0,domain=-9:10,variable=t]({ 3 },{ t });

     \addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=-1:3}];

     \node[above right] at (0,0) {\scalebox{0.5}{$O$}};
     \node[below left] at (-1,0) {\scalebox{0.5}{$-1$}};
     \node[below right] at (3,0) {\scalebox{0.5}{$3$}};

  \end{axis}
 \end{tikzpicture}
\end{figure}

But it seems that the command \addplot[gray!30,opacity=0.6] fill between[of=A and B, soft clip={domain=-1:3}] doesn't have any effect at my example.

How can I fix this ?

enter image description here

Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
Cris
  • 1,189
  • I don't know why, but if you remove samples=1001 form the definiton of A and B, it works. Seems that the limit is between 100 and 200; when it fails, it says: Package pgf Warning: fill between skipped: the first input path is empty. on in put line 28. – Rmano Dec 22 '17 at 14:37
  • Also if you remove the soft clip it works, whichever the number of samples. Puzzling... – Rmano Dec 22 '17 at 14:40
  • 1
    please, privide complete small document -- mwe -- not only code snippet ... – Zarko Dec 22 '17 at 14:44
  • Without checking it it sounds like the already reported bug https://sourceforge.net/p/pgfplots/bugs/194. – Stefan Pinnow Dec 22 '17 at 14:54
  • this is a duplicate of https://sourceforge.net/p/pgfplots/bugs/139/ and will work out of the box with the next release (1.16) of pgfplots – Christian Feuersänger Dec 22 '17 at 15:37
  • Thank you for all answers ! With samples=101 is working well, indeed. – Cris Dec 22 '17 at 16:11

2 Answers2

5

While you are waiting for the bug to be fixed, here's an effort in Metapost, which you can compile with lualatex (or adapt for plain MP, or the GMP package).

enter image description here

\RequirePackage{luatex85}
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
    numeric u, s;
    u = 5mm;

    path xx, yy, pp, A, B, S;
    xx = (left--right) scaled 8u;
    yy = xx rotated 90;

    s = 1/8;
    pp = ( (-1,1) for x = s-1 step s until 3: -- (x,x**2) endfor ) scaled u;

    A = pp shifted (0,u);
    B = reverse pp reflectedabout(left, right);
    S = A -- B -- cycle;

    fill S withcolor 3/4 white;
    drawarrow xx withcolor 1/4 white;
    drawarrow yy withcolor 1/4 white;
    draw S;

    label.rt("$x$", point 1 of xx);
    label.top("$y$", point 1 of yy);

    label.llft("$-1$", (-u,0));
    label.lrt("$3$", (3u,0));

endfig;
\end{mplibcode}
\end{document}
Thruston
  • 42,268
  • Just out of curiosity, what is the reason for the parentheses around the second x in (x,(x)**2)? – Franck Pastor Dec 23 '17 at 17:42
  • @FranckPastor Superstition :-) Just to make sure we get (-1)**2 instead of -(1**2); but in fact MP's precedence rules don't make this error, so they are not necessary. – Thruston Dec 23 '17 at 18:35
3

While you are waiting for the bug to be fixed, you can use pgfplots without soft clip macro:

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections, positioning}

\begin{document}
    \begin{tikzpicture}[node distance=0pt,
                        N/.style={font=\tiny, inner sep=1pt}
                        ]
\begin{axis}[
    axis lines=middle,
    axis equal,
    xlabel=$x$,    ylabel=$y$,
    xtick=\empty,  ytick=\empty,
    xmin=-2,  xmax=4,
    ymin=-10, ymax=10,
    no marks,
    samples=101,
    domain=-1:3
    ]
 \addplot[thick,name path=A] {-x^2};
 \addplot[thick,name path=B] { x^2+1};

 \draw[thick] (axis cs:-1,-1) -- (axis cs:-1,2);
 \draw[thick] (axis cs: 3,-9) -- (axis cs:3,10);

 \addplot[gray!30,opacity=0.6] fill between[of=A and B];

 \node[N, above right=of {axis cs: 0,0}]  {$ 0$};
 \node[N, below  left=of {axis cs:-1,0}]  {$-1$};
 \node[N, below right=of {axis cs: 3,0}]  {$ 3$};
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Zarko
  • 296,517