3

I am trying to plot functions and shade in between them. Here is the code:

\documentclass[a4paper, 12pt]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
% I included the formatting stuff as well
\usepackage{fancyhdr}
\usepackage[a4paper, portrait, margin=1in]{geometry}

\pagestyle{fancy}
\fancyhf{}
\rhead{October 18, 2018}
\lhead{Tutoring Notes}
\rfoot{\thepage}

\begin{document}

\begin{figure}[ht]

\centering

\begin{tikzpicture}[scale=1.75,line width=1pt]
\begin{axis}[
color= gray,
xmin=-4.9, 
xmax=4.9, 
ymin=-1.9, 
ymax=2.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
xticklabels={}, 
yticklabels={},
legend pos=outer north east,
inner axis line style={stealth-stealth}
]

\addplot[red, smooth, domain=-4.9:4.9, name path=1] {(8)/(4 + x^2)}; 
\addlegendentry[black]{$f(x)$}

\addplot[blue, smooth, domain=-4.9:4.9, name path=2]{(-16*x)/((4+x^2)^2)};
\addlegendentry[black]{$f'(x)$}

\addplot[green, smooth, domain=-4.9:4.9, name path=3]{-(((16)*((4 + 
x^2)^2))-((4 + x^2)*(64*(x^2))))/((4+x^2)^4)};
\addlegendentry[black]{$f''(x)$}

%\addplot[red, fill opacity=0.20] fill between [of=1 and 2,soft clip= 
{domain=2:4}];

\draw[black] (0,1) circle [radius=1];


\end{axis}
\end{tikzpicture}

\caption{Plotting the curve when $r=1$.}
\label{fig:1}

\end{figure}

\end{document}

And when the one line is commented out, I get a graph in the desired location (this is part of a bigger document):

good location

However, when I uncomment the plot command that is meant to shade in a region between two of the curves, there is a huge skip in the document and the legend disappears:

bad location

Why is this happening? This happens when I plot other functions as well, and I do not know why.

akenny430
  • 1,389
  • I tried to find a solution, but couldn't. Testing a few things showed using the domain 0:4 for the soft clip, something else goes wrong. A domain of 0.0001:4 is OK. Weird. It was @marmot to the rescue ... – corporal Oct 31 '18 at 03:09

1 Answers1

3

I do not really know what's going on. Chances are that there is a relation to these issues, where fillbetween also shifts some coordinates. Compiling your plot as a standalone revealed that your legend got moved to the moon, something that you cannot appreciate when you have an A4 sized paper. This effectively shifts the plot (and makes the legend disappear). And it suggests the following workaround, which moves the legend to a position where it makes more sense.

\documentclass[a4paper, 12pt]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{fillbetween}
% I included the formatting stuff as well
\usepackage{fancyhdr}
\usepackage[a4paper, portrait, margin=1in]{geometry}

\pagestyle{fancy}
\fancyhf{}
\rhead{October 18, 2018}
\lhead{Tutoring Notes}
\rfoot{\thepage}

\begin{document}

\begin{figure}[ht]

\centering

\begin{tikzpicture}[scale=1.75,line width=1pt]
\begin{axis}[
color= gray,
xmin=-4.9, 
xmax=4.9, 
ymin=-1.9, 
ymax=2.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
xticklabels={}, 
yticklabels={},
%legend pos=outer north east,
legend to name=named,
inner axis line style={stealth-stealth}
]
\addplot[red, smooth, domain=-4.9:4.9, name path=1] {(8)/(4 + x^2)}; 
\addlegendentry[black]{$f(x)$}

\addplot[blue, smooth, domain=-4.9:4.9, name path=2]{(-16*x)/((4+x^2)^2)};
\addlegendentry[black]{$f'(x)$}

\addplot[green, smooth, domain=-4.9:4.9, name path=3]{-(((16)*((4 + 
x^2)^2))-((4 + x^2)*(64*(x^2))))/((4+x^2)^4)};
\addlegendentry[black]{$f''(x)$}

\addplot[red, fill opacity=0.20] fill between [of=2 and 1,soft clip= 
{domain=2:4}];

\draw[black] (0,1) circle [radius=1];

\end{axis}
\node at (current axis.north east) {\ref{named}};
\end{tikzpicture}

\caption{Plotting the curve when $r=1$.}
\label{fig:1}

\end{figure}
\end{document}

enter image description here

Of course, a true solution of the problem is more desirable. I guess that apart from Christian Feuersänger, the author of pgfplots, there might not be too many who can provide it.