5

I want to display three plots each stacked vertically. As shown below in the figure. Generated by the following code. But fillbetween is not working.

WithScope

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
\begin{scope}
    \begin{axis}[
    % Axis Line Passing Through Middle
    axis x line = middle,
    axis y line = middle,
    % X and Y labels
    xlabel={$t$},
    ylabel={$x(t) = x_{1}(t)x_{2}(t)$},
    % Positioning X and Y label
    every axis x label/.style={
        at={(ticklabel* cs:1)},
        anchor=west,
    },
    every axis y label/.style={
        at={(ticklabel* cs:1)},
        anchor=south,
    },
    % Fonts to be Tiny
    font=\tiny,
    % Abscissa Range
    xmin = -1.8,
    xmax = 1.8,
    % Ordinate Range
    ymin = -1,
    ymax = 1,
    % Extending Axis Range
    enlarge x limits = 0.15,
    enlarge y limits = 0.15,
    % Axis Line Style to be very thin
    axis line style = very thin,
    % Axis Line Style Color
    axis line style = {black!50!white},
    % Plot Dimension
    width=10cm,
    height=3.5cm,
    ]
    % Add plot from data file with cyan color and assigning it to path A
    \addplot[color = cyan,domain = -2:2,samples = 400,name path = 
    A,smooth]{cos(deg(2*pi*x))*cos(deg(3*pi*x))};

    % Reference path B for the domain 0 to 0.17 with value set to 0
    \addplot+[draw=none,name path = B, domain=0:1, mark=none] {0};

    % Fill area between path A and B for the domain 0 to 0.17
    \addplot+[yellow, fill opacity = 0.4] fill between[of = A and       
    B,soft 
    clip = {domain = 0:0.17}];

    % Fill area between path A and B for the domain 0.17 to 0.25
    \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
    clip = {domain = 0.17:0.25}];

    % Fill area between path A and B for the domain 0 to 0.17
    \addplot+[yellow, fill opacity = 0.4] fill between[of = A and 
    B,soft 
    clip = {domain = 0.25:0.5}];

    % Fill area between path A and B for the domain 0 to 0.17
    \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
    clip = {domain = 0.5:0.75}];

    % Fill area between path A and B for the domain 0.75 to 0.83
    \addplot+[yellow, fill opacity = 0.4] fill between[of = A and 
    B,soft 
    clip = {domain = 0.75:0.83}];

    % Fill area between path A and B for the domain 0 to 0.17
    \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
    clip = {domain = 0.83:1}];

    % Placing Origin 0
    \node[] at (axis cs: -0.1,-0.35) {$0$};
    % Placing \cdots for continuing right side
    \node at (axis cs:2.2,0.5) [anchor=north east] {$\cdots$};
    % Placing \cdots for continuing left side
    \node at (axis cs:-2.2,0.3) [anchor=south west] {$\cdots$};
    \end{axis}
\end{scope}

\begin{scope}[yshift=2.5cm]
    \begin{axis}[
        axis x line = middle,
        axis y line = middle,
        xlabel={$t$},
        ylabel={$x_{2}(t)$},
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=west,
        },
        every axis y label/.style={
            at={(ticklabel* cs:1)},
            anchor=south,
        },
        font=\tiny,
        xmin = -1.8,
        xmax = 1.8,
        enlarge x limits = 0.15,
        enlarge y limits = 0.15,
        ymin = -1,
        ymax = 1,
        axis line style = {black!50!white},
        axis line style = very thin,
        width=10cm,
        height=3.5cm,
        ]
        \addplot[color = cyan,domain = -2:2,samples = 400,name path = 
        A,smooth]{cos(deg(2*pi*x))};

        % Placing Origin 0
        \node[] at (axis cs: -0.1,-0.35) {$0$};
        % Placing \cdots for continuing right side
        \node at (axis cs:2.2,0.5) [anchor=north east] {$\cdots$};
        % Placing \cdots for continuing left side
        \node at (axis cs:-2.2,0.3) [anchor=south west] {$\cdots$};
    \end{axis}
\end{scope}

\begin{scope}[yshift=5cm]
    \begin{axis}[
        axis x line = middle,
        axis y line = middle,
        xlabel={$t$},
        ylabel={$x_{1}(t)$},
        every axis x label/.style={
            at={(ticklabel* cs:1)},
            anchor=west,
        },
        every axis y label/.style={
            at={(ticklabel* cs:1)},
            anchor=south,
        },
        font=\tiny,
        xmin = -1.8,
        xmax = 1.8,
        enlarge x limits = 0.15,
        enlarge y limits = 0.15,
        ymin = -1,
        ymax = 1,
        axis line style = {black!50!white},
        axis line style = very thin,
        width=10cm,
        height=3.5cm,
        ]
        \addplot[color = cyan,domain = -2:2,samples = 400,name path = 
        A,smooth]{cos(deg(3*pi*x))};

        % Placing Origin 0
        \node[] at (axis cs: -0.1,-0.35) {$0$};
        % Placing \cdots for continuing right side
        \node at (axis cs:2.2,0.5) [anchor=north east] {$\cdots$};
        % Placing \cdots for continuing left side
        \node at (axis cs:-2.2,0.3) [anchor=south west] {$\cdots$};
    \end{axis}
\end{scope}
\end{tikzpicture}
\end{document}

Whereas if I'm not using scope fillbetween works as desired.

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % Axis Line Passing Through Middle
        axis x line = middle,
        axis y line = middle,
        % X and Y labels
        xlabel={$t$},
        ylabel={$x(t) = x_{1}(t)x_{2}(t)$},
        % Positioning X and Y label
        every axis x label/.style={
                at={(ticklabel* cs:1)},
                anchor=west,
                },
        every axis y label/.style={
                at={(ticklabel* cs:1)},
                anchor=south,
                },
        % Fonts to be Tiny
        font=\tiny,
        % Abscissa Range
        xmin = -1.8,
        xmax = 1.8,
        % Ordinate Range
        ymin = -1,
        ymax = 1,
        ytick = {\empty},
        % Extending Axis Range
        enlarge x limits = 0.15,
        enlarge y limits = 0.15,
        % Axis Line Style to be very thin
        axis line style = very thin,
        % Axis Line Style Color
        axis line style = {black!50!white},
        % Plot Dimension
        width=10cm,
        height=3.5cm,
        ]
        % Add plot from data file with cyan color and assigning it to path A
        \addplot[color = cyan,domain = -2:2,samples = 400,name path = 
        A,smooth]{cos(deg(2*pi*x))*cos(deg(3*pi*x))};


        % Reference path B for the domain 0 to 0.17 with value set to 0
        \addplot+[draw=none,name path = B, domain=0:1, mark=none] {0};

        % Fill area between path A and B for the domain 0 to 0.17
        \addplot+[yellow, fill opacity = 0.4] fill between[of = A and       
        B,soft 
        clip = {domain = 0:0.17}];

        % Fill area between path A and B for the domain 0.17 to 0.25
        \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
        clip = {domain = 0.17:0.25}];

        % Fill area between path A and B for the domain 0 to 0.17
        \addplot+[yellow, fill opacity = 0.4] fill between[of = A and 
        B,soft 
        clip = {domain = 0.25:0.5}];

        % Fill area between path A and B for the domain 0 to 0.17
        \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
        clip = {domain = 0.5:0.75}];

        % Fill area between path A and B for the domain 0.75 to 0.83
        \addplot+[yellow, fill opacity = 0.4] fill between[of = A and 
        B,soft 
        clip = {domain = 0.75:0.83}];

        % Fill area between path A and B for the domain 0 to 0.17
        \addplot+[red, fill opacity = 0.4] fill between[of = A and B,soft 
        clip = {domain = 0.83:1}];



        % Placing Origin 0
        \node[] at (axis cs: -0.1,-0.35) {$0$};

        % Plaacing \cdots for continuing
        \node at (axis cs:2.2,0.5) [anchor=north east] {$\cdots$};
        \node at (axis cs:-2.2,0.3) [anchor=south west] {$\cdots$};
    \end{axis}
\end{tikzpicture}
\end{document}

Without Scope

Am I doing any mistake!

8

This is because "scoping" is not allowed when layers are used which is true when you use fill between. As the PGFPlots manual (v1.15) states in section 4.27.2 (on page 407)

Limitation: no environments or local TeX groups allowed. Standard usages as within the examples of this manual will always work. But since the layer name configuration is essentially part of a pgf picture (at a low level), one cannot arbitrarily set them; pgf will complain if they are changed within some nested TeX groups or LaTeX environments. Typically, you will never need to worry about this.

In short, the following examples are forbidden because the axis is within locally nested groups.

which is followed by 3 examples. To circumvent this problem you could just move the yshift command to the axis options. This is part of my provided solution given below.

In addition you can heavily simplify your code by

  • creating a style which is used for every axis environment, and
  • by using the split option for your fill between code.

For details, please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{fillbetween}
    \pgfplotsset{
        my axis style/.style={
            % Axis Line Passing Through Middle
            axis x line=middle,
            axis y line=middle,
            % X and Y labels
            xlabel={$t$},
            % Positioning X and Y label
            every axis x label/.style={
                at={(ticklabel* cs:1)},
                anchor=west,
            },
            every axis y label/.style={
                at={(ticklabel* cs:1)},
                anchor=south,
            },
            % Fonts to be Tiny
            font=\tiny,
            % Abscissa Range
            xmin=-1.8,
            xmax=1.8,
            % Ordinate Range
            ymin=-1,
            ymax=1,
            % Extending Axis Range
            enlarge x limits=0.15,
            enlarge y limits=0.15,
            % Axis Line Style to be very thin
            axis line style=very thin,
            % Axis Line Style Color
            axis line style={black!50!white},
            % Plot Dimension
            width=10cm,
            height=3.5cm,
            % -----------------------------------------------------------------
            % moved common stuff here
            domain=-2:2,
            samples=201,
            mark=none,
            % moved stuff that is added to every plot here
            execute at begin axis={
                % Placing Origin 0
                \node[] at (axis cs: -0.1,-0.35) {$0$};
                % Placing \cdots for continuing right side
                \node at (axis cs:2.2,0.5) [anchor=north east] {$\cdots$};
                % Placing \cdots for continuing left side
                \node at (axis cs:-2.2,0.3) [anchor=south west] {$\cdots$};
            },
            % -----------------------------------------------------------------
        },
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        my axis style,
        ylabel={$x(t)=x_{1}(t)x_{2}(t)$},
        % ---------------------------------------------------------------------
        % added stuff
        name=lower plot,
        % ---------------------------------------------------------------------
    ]
        % Add plot from data file with cyan color and assigning it to path A
        \addplot [color=cyan,name path=A]
            {cos(deg(2*pi*x))*cos(deg(3*pi*x))};

        % Reference path B for the domain 0 to 0.17 with value set to 0
        \addplot+ [draw=none,mark=none,name path=B,domain=0:1] {0};

        \addplot fill between [
            of=A and B,
            soft clip={domain=0:1},
            split,
            every odd segment/.style={
                red, fill opacity=0.4,
            },
            every even segment/.style={
                yellow, fill opacity=0.4,
            },
        ];
    \end{axis}

    \begin{axis}[
        my axis style,
        ylabel={$x_{2}(t)$},
        % ---------------------------------------------------------------------
        % added stuff
        name=middle plot,
        anchor=south,
        at={(lower plot.above north)},
        yshift=5pt,
        % ---------------------------------------------------------------------
    ]
        \addplot [color=cyan] {cos(deg(2*pi*x))};
    \end{axis}

    \begin{axis}[
        my axis style,
        ylabel={$x_{1}(t)$},
        % ---------------------------------------------------------------------
        % added stuff
        name=upper plot,
        anchor=south,
        at={(middle plot.above north)},
        yshift=5pt,
        % ---------------------------------------------------------------------
    ]
        \addplot [color=cyan] {cos(deg(3*pi*x))};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • I'm getting error at line 56 with Package pgfkeys Error: Choice '1.15' unknown in choice key '/pgfplots/compat/anchors'. I am going to ignore this key. } – Meet Feb 26 '18 at 18:21
  • 1
    This was because of the line compat=1.15 in the \pgfplotsset which I just removed. (I added this to use the Lua features which allowed faster compilation.) Most likely you have an older version of PGFPlots which causes this error message. – Stefan Pinnow Feb 26 '18 at 18:33
  • I tried the same after removing compat=1.15 in the \pgfplotsset. But still, the plots are same as that of mine without filling. – Meet Feb 26 '18 at 18:38
  • Sorry, I just noticed that I was using the current CVS version of PGFPlots where bug 139 already is solved. So reducing the number of samples from 401 to 201 now gives the desired result (see my edited answer). – Stefan Pinnow Feb 26 '18 at 18:49

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.