This is related to another questions I posted (Shading around circles and ellipses). As I have only gotten a partial answer, I wanted to post a more narrow question to see if I can get the rest figured out.

I have a system of nonlinear equations that yield a circle inside an ellipse. The region of feasibility is inside the circle and outside the ellipse. I found a satisfactory answer for shading inside the circle in my previous question, which will be shown in the answer below.

I am wanting to extend the result to shading outside the ellipse as well. I want to be clear, using filldraw for the surrounding rectangle (the entire axis) does not work (as I have been able to implement it) as it leaves a "haziness" to the axis numbers.

I would prefer to work inside the axis environment as well and keep my functions defined as they are. This would give an approach that would be more flexible for future examples.

In essence the workaround for systems of linear equations is to make a path at the top or bottom of the axis and use them in fillbetween. I am hoping for a similar result here.

The below image shows my current picture. You can see the "haziness" mentioned. I want that region to not be shaded instead.

\documentclass[border=10pt]{standalone}
\usepackage{amsmath,amsthm}
\usepackage{tikz,pgfplots}     %for graphics
\usepgfplotslibrary{fillbetween}
\usepackage{xcolor}

\begin{document}
\pagestyle{empty}

\pagecolor{green}

\begin{tikzpicture}
\begin{axis}[
xmin=-6, xmax=6, ymin=-6, ymax=6,
axis lines=middle,
ticklabel style={font=\tiny},
xtick = {-5,-4,...,5},
ytick = {-5,-4,...,5},
xlabel = $$x$$,
ylabel = $$y$$,
legend style={legend pos=north east,font=\tiny}
]

domain=-pi:pi,
samples=200]({5*sin(deg(x))}, {3*cos(deg(x))});
\addlegendentry{$$\frac{x^{2}}{25} + \frac{y^{2}}{9} = 1$$}

domain=-pi:pi,
samples=200]({3*sin(deg(x))}, {3*cos(deg(x))});
\addlegendentry{$$x^{2} + y^{2} = 9$$}

\path[name path = C] (0,0) -- (0,0);

\filldraw[blue!70,opacity=0.4] (-6,-6) rectangle (6,6);

\filldraw[green,opacity=0.4] (0,0) ellipse (5 and 3);

\addplot[blue!70,opacity=0.4] fill between [of=B and C];
\end{axis}
\end{tikzpicture}

\end{document}


• Could you add pictures showing both the current output as well as the one you intend? Is path C supposed to be (-6,0) -- (6,0)? Commented Dec 8, 2022 at 23:32
• I removed that line. It was an artifact from a previous attempt I had made. I tried to add the image and it shows up when I go to edit...hopefully it will appear now. Commented Dec 9, 2022 at 14:25

If you do not want your fill to cover your axis, then use the option axis on top. No need to use layers(explicit), opacity or fillbetween.

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-6, xmax=6,
ymin=-6, ymax=6,
axis lines=middle,
ticklabel style={font=\tiny},
xtick={-5,-4,...,5},
ytick={-5,-4,...,5},
xlabel=$$x$$, ylabel=$$y$$,
legend style={legend pos=north east, font=\tiny},
axis on top,
]
\fill[green!40, even odd rule]
(current axis.south west) rectangle (current axis.north east)
plot[domain=-pi:pi, samples=50, smooth] ({5*sin(deg(\x))}, {3*cos(deg(\x))}) -- cycle
plot[domain=-pi:pi, samples=50, smooth] ({3*sin(deg(\x))}, {3*cos(deg(\x))}) -- cycle;
\addlegendentry{$$\frac{x^{2}}{25} + \frac{y^{2}}{9}=1$$};
\addlegendentry{$$x^{2} + y^{2}=9$$}
\end{axis}
\end{tikzpicture}
\end{document}


• I selected this solution. It required the least amount of modification and was the most flexible. This is what I was looking for. Thank you! Commented Dec 12, 2022 at 16:19

Perhaps use of layers and clipping would suit you:

\documentclass[border=10pt]{standalone}
\usepackage{amsmath,amsthm}
\usepackage[dvipsnames]{xcolor}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pagecolor{Dandelion!20}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-6, xmax=6, ymin=-6, ymax=6,
axis lines=middle,
ticklabel style={font=\tiny},
xtick = {-5,-4,...,5},
ytick = {-5,-4,...,5},
xlabel = $$x$$,
ylabel = $$y$$,
legend style={legend pos=north east,font=\tiny},
layers/my layer set/.define layer set={
pre main,
main}{},
set layers=my layer set,
]
name path=A,
domain=-pi:pi,
samples=200
]({5*sin(deg(x))}, {3*cos(deg(x))});
\addlegendentry{$$\frac{x^{2}}{25} + \frac{y^{2}}{9} = 1$$}
name path=B,
domain=-pi:pi,
samples=200
]({3*sin(deg(x))}, {3*cos(deg(x))});
\addlegendentry{$$x^{2} + y^{2} = 9$$}
\path[name path = C] (0,0) -- (0,0);