4

I am trying to shade the area restricted by the functions y \sin x, x = \sin y and y = 2pi + x. Now my problem is that I do not have access to Pgfplots 1.10so the libary fillbetween is not accessible to me. I have attempted to plot the area below using

\documentclass[a4paper,12pt]{standalone} 
\usepackage{pgfplots}
%\pgfplotsset{compat=1.10}
%\usepgfplotslibrary{fillbetween}

\begin{document}
\pgfplotsset{every axis legend/.append style={
at={(0,0.99)},
anchor=north west}} 
\begin{tikzpicture}
\begin{axis}[
    xmin=-2.5*pi,xmax=pi,ymin=-pi,ymax=2.5*pi,
    domain=-2pi:2pi,
    samples=100,
    xlabel={$x$},  
    ylabel={$y$},
    yticklabels={,,},
    xticklabels={,,},
    axis lines=middle
]
\addlegendentry{$y =x + 2\pi$}
\addplot[black,thick,black,domain=-2*pi:0] {x+2*pi} \closedcycle;
\addplot[fill=gray,opacity=0.1,thick,black,domain=-2*pi:0] {x+2*pi} \closedcycle;

\addplot[color=red,fill= white,thick,domain=-2*pi:0] {sin(deg(x))};
\addplot[color=blue,fill= white,thick,variable=\t, domain=0:2*pi] ({sin(deg(t))}, {t});

\addplot[color=red,fill=gray,opacity=0.2,thick,domain=-pi:0] {sin(deg(x))};
\addplot[color=blue,fill=gray,opacity=0.2,thick,variable=\t, domain=0:pi] ({sin(deg(t))}, {t});

\addlegendentry{$y =x + 2\pi$}
\addplot[color=red,thick,domain=-2*pi:0] {sin(deg(x))};
\addlegendentry{$y =x + 2\pi$}
\addplot[color=blue,thick,variable=\t, domain=0:2*pi] ({sin(deg(t))}, {t});
%\addlegendentry{$x = \sin y$}
]\end{axis}
\end{tikzpicture}
\end{document}

TheBowler

I have tried to mark the parts on the figure I am having problems with. All of them are related to how I am shading the figure. The idea I am using is to first fill in the entire triangular area under x + 2\pi, and then fill in white where needed.

  • Why must i use opacity=0.1 for the first function and opacity=0.2 for the second function to obtain the same color gray? Is this a problem on my end?
  • Why is the x-axis unneven, eg higher between -pi and 0 ? Same with y-axis.
  • Labels, why are the first and last label correct, but the middle one is always gray?

I think the labeling problem is a minor problem caused by how I fill the various parts of the figure. But still it is quite weird that the first and second legends are fine. So to summarize, is there a better method for shading the area gray? I have tried the method provided here filling area between 2 functions, with shading, but alas this crashes every time I try to invoke the sine function, and not sure how to handle the implicit function using this method.

5

To answer your three points briefly:

  • There is an extra black after the in the first function. I suppose this overrides the defined fill colour (gray), and black with opacity=0.1 is about the same as gray with opacity=0.2.
  • It is because of the filling I think, which covers up half of the axis width. For x < -pi/2 the top half is covered, for x > -pi/2 the bottom half is covered, leading to the small 'step'. You can fix this by placing the fill on the background layer.
  • \addlegendentry picks up the plots in the order they are defined, regardless of where they are placed, so it is the first three \addplots that are shown in the legend. The forget plot key removes a plot from the legend I believe.

You have a spurious ] just before \end{axis} by the way.

In the code below I plot the lines first, then add the fill on the background layer using the backgrounds library from TikZ.

\documentclass[a4paper,12pt,border=3mm]{standalone} 
\usepackage{pgfplots}
%\pgfplotsset{compat=1.10}
\usetikzlibrary{backgrounds}
\begin{document}
\pgfplotsset{every axis legend/.append style={
at={(0,0.99)},
anchor=north west},
cells={anchor=west}} 
\begin{tikzpicture}
\begin{axis}[
    xmin=-2.5*pi,xmax=pi,ymin=-pi,ymax=2.5*pi,
    domain=-2pi:2pi,
    samples=100,
    xlabel={$x$},  
    ylabel={$y$},
    yticklabels={,,},
    xticklabels={,,},
    axis lines=middle
]

\addplot[draw=black,thick,domain=-2*pi:0] {x+2*pi};
\addplot[draw=red,thick,domain=-2*pi:0] {sin(deg(x))};
\addplot[draw=blue,thick,variable=\t, domain=0:2*pi] ({sin(deg(t))}, {t});
\addlegendentry{$y = x + 2\pi$}
\addlegendentry{$y = \sin x$}
\addlegendentry{$x = \sin y$}

\begin{scope}[on background layer]
\addplot[draw=none,fill=gray,fill opacity=0.2,domain=-2*pi:0] {x+2*pi} \closedcycle;
\addplot[draw=none,fill= white,domain=-2*pi:0] {sin(deg(x))};
\addplot[draw=none,fill= white,variable=\t, domain=0:2*pi] ({sin(deg(t))}, {t});
\addplot[draw=none,fill=gray,fill opacity=0.2,thick,domain=-pi:0] {sin(deg(x))};
\addplot[draw=none,fill=gray,fill opacity=0.2,thick,variable=\t, domain=0:pi] ({sin(deg(t))}, {t});
\end{scope}

\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.