43

In order to fill the area between two complex curves I use tables and the vertcat command (see Fill between two curves in pgfplots.).

Now I don't want the plotted area to appear in my legend, just the two curves. Is there any command to prevent the yellow area plot from being added to my legend? this could be a minimal example:

\documentclass{article} \usepackage{pgfplots} 
\begin{document} \begin{tikzpicture}
  \begin{axis}[area style,axis on top]
    \addplot+[mark=none,fill=yellow,draw=none] {0.1*x^2} \closedcycle;
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
    \legend{red curve,blue curve};
    % \legend{\empty,red curve,blue curve};
  \end{axis}
\end{tikzpicture} \end{document}

With this legend{...}-command the last plot is skipped, an additional \empty doesn't help, too.

Sadly, drawing the first to plots together with fill=yellow,draw=red is not an option for me. Does anybody know any inside hacks? As far as I know, there is no command shown in the manual. Thanks in advance.

66

Adding the option forget plot to the plot you do not want shown does the trick.

\documentclass{article} \usepackage{pgfplots} 
\begin{document} \begin{tikzpicture}
  \begin{axis}[area style,axis on top]
    \addplot+[mark=none,fill=yellow,draw=none,forget plot] {0.1*x^2} \closedcycle;
    \addplot[mark=none,draw=red,line legend] {0.1*x^2};
    \addplot[mark=none,draw=blue,line legend] {0.0*x^2};
    \legend{red curve,blue curve};
  \end{axis}
\end{tikzpicture} \end{document}

forget plot pgfplots


Alternatively, you could use an empty entry in the \legend list for the plot you want to exclude from the legend:

\documentclass{article}
\usepackage{pgfplots} 
\begin{document}
\begin{tikzpicture}
  \begin{axis}[axis on top]
    \addplot [area style,fill=yellow,draw=none] {0.1*x^2} \closedcycle;
    \addplot [draw=red] {0.1*x^2};
    \addplot [draw=blue] {0.0*x^2};
    \legend{,red curve,blue curve};
  \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |
  • 3
    Is forget plot the only way to avoid a legend. The reason I ask is because forget plot has other side effects like for example that symbol and color counter doesn't increase. – alfC Feb 3 '14 at 10:01
  • 2
    @alfC: You can use empty entries in the \legend list to exclude plots. I've edited my answer. – Jake Feb 3 '14 at 10:06
  • It's important also remove the NAN points if exist in order to avoid errors – Bruce_Warrior Aug 3 '17 at 9:11
2

I arrived at this question when I had a similar, but not quite the same problem. I'll include my problem and answer here in case others (or myself in the future) have the same problem and land here.

My problem: I have vertically stacked plots, on the top plot I have 4 series (let's assume they are '1' in red, '2' in blue, '3' in green and '4' in black). I have a legend on the top plot, labelling all 4 series.

In my subsequent plots I only plot some of the series (say '2' and '4'). The problem is that I'm using a cycle list to define the plot styles, so I effectively want to 'skip' a plot style before plotting 2, and between plotting 2 and 4.

I haven't found a very satisfactory way of doing this, but one work-around that works for me is to plot nan (not a number) data for the plots I don't want to appear.

Stand-alone example of this work-around given below:

\documentclass{article}
\usepackage{pgfplots} 
\begin{document}

    \pgfplotscreateplotcyclelist{my plot list}{
        color=red\\
        color=blue\\
        color=green\\
        color=black\\}

\begin{tikzpicture}
  \begin{axis}[ axis on top,
                height=0.8\linewidth,
                width=0.8\linewidth,
                at={(0.5\linewidth,0.35\linewidth)},
                cycle list name=my plot list,
                xticklabels={\empty}]
    \addplot
        table[row sep=crcr]{%
            0.1 0.0\\
            0.2 0.0\\
            0.3 0.0\\
        };

     \addplot
         table[row sep=crcr]{%
            0.1 0.1\\
            0.2 0.2\\
            0.3 0.3\\
         };

     \addplot
         table[row sep=crcr]{%
            0.1 0.2\\
            0.2 0.4\\
            0.3 0.6\\
         };     

     \addplot
         table[row sep=crcr]{%
            0.1 -0.1\\
            0.2 -0.2\\
            0.3 -0.3\\
         };

    \legend{1, 2, 3, 4};
  \end{axis}

  \begin{axis}[ axis on top,
                height=0.8\linewidth,
                width=0.8\linewidth,
                at={(0.5\linewidth,-0.35\linewidth)},
                cycle list name=my plot list]

    \addplot
        table[row sep=crcr]{%
            nan nan\\
        };

    \addplot
        table[row sep=crcr]{%
            0.1 0.1\\
            0.2 0.2\\
            0.3 0.3\\
        };

    \addplot
        table[row sep=crcr]{%
            nan nan\\
        };      

    \addplot
        table[row sep=crcr]{%
            0.1 -0.1\\
            0.2 -0.2\\
            0.3 -0.3\\
        };

  \end{axis}

\end{tikzpicture}
\end{document}
| improve this answer | |
  • Welcome to TeX - LaTeX! The nan idea is novel. – Andrew Swann Aug 22 '16 at 5:57
  • look for cycle list shift in the manual – percusse Aug 22 '16 at 6:06
  • Thanks @percusse that was the command I was looking for! However, the above work-around is still useful for me (and perhaps others) because I'm using the matlab2tikz tool for producing my plots, and can easily include NaN plots from matlab, to get the desired behaviour. – kabdulla Aug 23 '16 at 0:42

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.