2

I have a series of plots that are filled with fill between. After I produce the plots, I draw a line which goes from the center of the area to an external point. I would like the line to disappear under a certain area. Until now I was able to produce just this

enter image description here

based on the following instructions

\documentclass[10pt]{article}

\usepackage{amsmath,bm}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{tikzscale}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}
\begin{axis}[domain=0:pi/2,
             xmin=-1.5, xmax=2]

\addplot[forget plot,
        name path=A, 
        thick,
        color=blue,
        ]  {sin(deg(x))};

\addplot[forget plot,
        name path=B, 
        thick,
        color=green,
        ]  {2*sin(deg(x))};
        
\addplot[forget plot,
        name path=C, 
        thick,
        color=red,
        ]  {3*sin(deg(x))};                
              
\addplot[forget plot, name path=axis,domain=0:pi/2]{0};

\addplot[red]   fill between[of=C and axis];
\addplot[green] fill between[of=B and axis];
\addplot[blue]  fill between[of=A and axis];

\addplot [forget plot, mark=none,  black] coordinates { (-0.5,0.5) (1,0.5)}; 

\addplot [forget plot, mark=none,  black] coordinates { (-0.5,1) (1,1)}; 

\addplot [forget plot, mark=none,  black] coordinates { (-0.5,2) (1,2)};
  
\node at (axis cs:-0.5,0.5) [anchor=east] {$sin(x)$};      
\node at (axis cs:-0.5,1) [anchor=east] {$2*sin(x)$};   
\node at (axis cs:-0.5,2) [anchor=east] {$3*sin(x)$};   

\end{axis}
\end{tikzpicture}

\end{document}
2
  • 1
    Sorry, but what do you mean by "disappear under a certain area"? Could you add a sketch to your image to show what you want to achieve? Mar 10, 2021 at 7:11
  • Hello Stefan, thank you for your reply. For example, I want the black line associated with sin(x) to be visible over the red and green are, while disappearing under the blue one, and so on for the other lines. Is it clear now?
    – Fly
    Mar 10, 2021 at 7:16

2 Answers 2

5

If I understood you comment to question correctly, than you after something like this:

enter image description here

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[domain=0:pi/2,
             xmin=-1.5, xmax=2,
             domain=0:pi/2,
             trig format=rad,
             ]
\addplot[name path=A, thick]     {sin(x)};
\addplot[name path=B, draw=none]   {2*sin(x)};
\addplot[name path=C, draw=none]    {3*sin(x)};

\addplot[name path=axis]            {0};

\addplot[red, semitransparent]   fill between[of=C and axis];
\addplot[green, semitransparent] fill between[of=B and axis];
\addplot[blue, semitransparent]  fill between[of=A and axis];

\draw   (1,0.50) -- (-0.5,0.50) node[left] {$\sin(x)$} 
        (1,1.25) -- (-0.5,1.25) node[left] {$2*\sin(x)$} 
        (1,2.00) -- (-0.5,2.00) node[left] {$3*\sin(x)$};
\end{axis}
    \end{tikzpicture}
\end{document}

For fill color is used semitransparent option that curve of function sin(x) is better visible. In diagram code I also took liberty for make it shorter and simpler.

Addendum (1): You may liked diagram with tick of function domain in shown in radians:

enter image description here

for this diagram you only need to add to axis option the following code lines:

             xtick={0, pi/6, pi/3, pi/2},
             xticklabels={0,$\pi/6$,$\pi/3$,$\pi/2$},
             xticklabel style = {font=\footnotesize},

Addendum (2): Considering OP comment below this answer, which make problem more clear, the one of possible (general) solution is employ ZikZ library intersections:

\documentclass[border=3.141592]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}
\begin{axis}[domain=0:pi/2,
             xmin=-1.5, xmax=2,
             xtick={0, pi/6, pi/3, pi/2},
             xticklabels={0,$\pi/6$,$\pi/3$,$\pi/2$},
             xticklabel style = {font=\footnotesize},
             domain=0:pi/2,
             trig format=rad,
every axis plot post/.append style={draw=none}             
            ]
\addplot[name path=A]   {sin(x)};
\addplot[name path=B]   {2*sin(x)};
\addplot[name path=C]    {3*sin(x)};

\addplot[name path=axis]            {0};

\addplot[red, semitransparent]   fill between[of=C and axis];
\addplot[green, semitransparent] fill between[of=B and axis];
\addplot[blue, semitransparent]  fill between[of=A and axis];

\path [name path=A1] (-0.5,0.50) node[left] (a) {$\sin(x)$} -- (pi/2,0.50); 
\path [name path=B1] (-0.5,1.25) node[left] (b) {$\sin(x)$} -- (pi/2,1.25);
\path [name path=C1] (-0.5,2.00) node[left] (c) {$\sin(x)$} -- (pi/2,2.00);

\draw [name intersections={of=A and A1, by=a1}] (a) -- (a1);
\draw [name intersections={of=B and B1, by=b1}] (b) -- (b1);
\draw [name intersections={of=C and C1, by=c1}] (c) -- (c1);
\end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Edit: Corrected are errors in naming of x ticks.

8
  • Hello Zarco, probably my request was not really clear. What I would like is the horizontal lines connecting one area to the labels (sin(x), 2*sin(x) or 3*sin(x)) to disappear under the filled area.
    – Fly
    Mar 10, 2021 at 8:05
  • It's then a problem of layers. This will definitely help tex.stackexchange.com/questions/350458/…
    – JeT
    Mar 10, 2021 at 11:14
  • @Fly, sorry, but now is obvious that your question is not clear at all.
    – Zarko
    Mar 10, 2021 at 11:14
  • 1
    @Fly, see addendum to my answer.
    – Zarko
    Mar 10, 2021 at 11:43
  • 1
    @Fly, with my code as are in addendum, this cannot be a case (tested again).
    – Zarko
    Mar 10, 2021 at 12:44
3

If I have well understood the request, you need to adjust the x coordinate so that it corresponds to a point on the graph. For this we have to use the inverse functions asin(y), asin(y/2) and asin(y/3)

\documentclass[10pt]{article}

\usepackage{amsmath,bm}
\usepackage{amssymb}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{math}
\usepackage{tikzscale}
\usepgfplotslibrary{fillbetween}

\begin{document}

\begin{tikzpicture}


\begin{axis}[domain=0:pi/2,
             xmin=-1.5, xmax=2]
             


\addplot[forget plot,
        name path=A, 
        thick,
        color=blue,
        ]  {sin(deg(x))};

\addplot[forget plot,
        name path=B, 
        thick,
        color=green,
        ]  {2*sin(deg(x))};
        
\addplot[forget plot,
        name path=C, 
        thick,
        color=red,
        ]  {3*sin(deg(x))};                
              
\addplot[forget plot, name path=axis,domain=0:pi/2]{0};

\addplot[red]   fill between[of=C and axis];
\addplot[green] fill between[of=B and axis];
\addplot[blue]  fill between[of=A and axis];

\addplot [forget plot, mark=none,  black] 
         coordinates { (-0.5,0.5) (rad(asin(0.5)),0.5)}; % <-----------

\addplot [forget plot, mark=none,  black] 
         coordinates { (-0.5,1) (rad(asin(1/2)),1)}; % <-------------

\addplot [forget plot, mark=none,  black] 
         coordinates { (-0.5,2) (rad(asin(2/3)),2)};% <-----------------
  
\node at(axis cs:-0.5,0.5)[anchor=east] {$sin(x)$};      
\node at (axis cs:-0.5,1) [anchor=east]{$2*sin(x)$};   
\node at (axis cs:-0.5,2) [anchor=east]{$3*sin(x)$};   

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

2
  • Hello Adif, thank you for the reply. It's exactly what I was searching, but what about a more general approach? In reality my curves are defined not by analytical functions.
    – Fly
    Mar 10, 2021 at 10:08
  • @Fly If the functions are defined by a list of coordinates then choose one of them and draw a line from this point to the text node Mar 10, 2021 at 11:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .