2

Ok,

Say we have a graph defined as

\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis x line=middle,
    axis y line=middle
    ]
    \addplot[black,domain=-pi/2:pi/2]{cos(deg(x))};
    \addplot[red,domain=-pi/2:pi/2]{1/64*pi^4-1/4*x^4};
    \end{axis}
    \end{tikzpicture}
\end{center}

This outputs the following:

Diagram without shading

Which is exactly what the 2 curves should look like, I now want to create a filled vertical region between these two curves and label it, like so

Ideal diagram!

What's the best way to achieve this effect? I've figured out the following with assistance of comment on this:

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{center}
    \begin{tikzpicture}
    \begin{axis}[
    axis x line=middle,
    axis y line=middle
    ]
    \addplot[name path=A,black,domain=-pi/2:pi/2]{cos(deg(x))};
    \addplot[name path=B, red,domain=-pi/2:pi/2]{1/64*pi^4-1/4*x^4};
    \addplot[color=black,fill=pink,fill opacity=0.4]fill between[of=A and B, soft clip={domain=-1:-0.5}];
    \end{axis}
    \end{tikzpicture}
\end{center}

Almost complete diagram

How do I put a border on the fill?

  • @JouleV I've update the question to ask another bit relating to what I've found. – whitelined Mar 22 at 14:23
4

JouleV already gave you the crucial hint. The rest is adding patterns, two vertical lines, and a node.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.16}
\begin{document}
  \begin{tikzpicture}[declare function={f(\x)=1/64*pi^4-pow(\x,4)/4;}]
    \begin{axis}[ymax=1.6,
    axis x line=middle,
    axis y line=middle
    ]
    \addplot[black,domain=-pi/2:pi/2,name path=A]{cos(deg(x))};
    \addplot[red,domain=-pi/2:pi/2,name path=B]{f(x)};
    \addplot [pattern=north east lines,pattern color=red] fill between [
        of=A and B,soft clip={domain=-1.1:-1},
    ];
    \pgfplotsinvokeforeach{-1.1,-1}{%
    \draw[red] (#1,{cos(deg(#1))}) -- (#1,{f(#1)});}
    \draw[red] (-1.05,{cos(deg(-1.05))})
     -- (-0.95,{cos(deg(-1.05))-0.1}) node[right,draw,text=black]{label};
    \end{axis}
  \end{tikzpicture}
\end{document}

enter image description here

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.