9

Found the answer: pgfplots: prevent single plot from being listed in legend

I plot multiple things to one tikzpicture. Because the plotting order is important to me, to avoid overlaying of the hatch over plotted lines, I need to plot the hatches first. But now I don’t want to show the labels for all the hatch-plots (only for one). How can I leave / hide some plots form legend or do I need to use a secondary axe?

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{patterns,arrows}
\begin{document}

\definecolor{plotrangecolor}{HTML}{D8D8D8} %{D8D8D8}

%%
%% https://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area-under-a-curve-with-oblique-lines-hatching-as-a
%%   
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}


\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}



\begin{tikzpicture}
\begin{axis}[
    xmin=1, xmax=15,
    domain=1:15,
    ymin=0, ymax=1.1,
    no markers,
    enlargelimits=false,
    xlabel={x},
    ylabel={y}, 
    legend style={
        cells={anchor=west},
        legend pos=south east
    }
    ]

\addplot[mark=none,
  domain=2:4,
  pattern=flexible hatch,
  hatch distance=10pt,
  hatch thickness=0.5pt,
  draw=none,
  pattern color=plotrangecolor,
  area legend] {2} \closedcycle;

\addplot[mark=none,
  domain=8:14,
  pattern=flexible hatch,
  hatch distance=10pt,
  hatch thickness=0.5pt,
  draw=none,
  pattern color=plotrangecolor,
  area legend] {2} \closedcycle;


\addplot  coordinates {(1,1) (4,0.4) (7,0.6) (8,0.5)     (14,0.2)};
\addplot  coordinates {(1,0.2)  (3,0.3)  (5,0.7)  (9,1)      (14,0.5)};


\addlegendentry{ range }   
\addlegendentry{ dont show this}     
  \addlegendentry{value 1}
  \addlegendentry{value 2}



\end{axis}
\end{tikzpicture}

\end{document}

Output of the Example

1 Answer 1

12

forget plot. This key leaves the \addplot in question out of the legend.

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{patterns,arrows}
\begin{document}

\definecolor{plotrangecolor}{HTML}{D8D8D8} %{D8D8D8}

%%
%% http://tex.stackexchange.com/questions/29359/pgfplots-how-to-fill-the-area-under-a-curve-with-oblique-lines-hatching-as-a
%%   
\tikzset{
hatch distance/.store in=\hatchdistance,
hatch distance=10pt,
hatch thickness/.store in=\hatchthickness,
hatch thickness=2pt
}


\makeatletter
\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}
{\pgfqpoint{0pt}{0pt}}
{\pgfqpoint{\hatchdistance}{\hatchdistance}}
{\pgfpoint{\hatchdistance-1pt}{\hatchdistance-1pt}}%
{
\pgfsetcolor{\tikz@pattern@color}
\pgfsetlinewidth{\hatchthickness}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{\hatchdistance}{\hatchdistance}}
\pgfusepath{stroke}
}



\begin{tikzpicture}
\begin{axis}[
    xmin=1, xmax=15,
    domain=1:15,
    ymin=0, ymax=1.1,
    no markers,
    enlargelimits=false,
    xlabel={x},
    ylabel={y}, 
    legend style={
        cells={anchor=west},
        legend pos=south east
    }
    ]

\addplot[mark=none,
  domain=2:4,
  pattern=flexible hatch,
  hatch distance=10pt,
  hatch thickness=0.5pt,
  draw=none,
  pattern color=plotrangecolor,
  area legend,
  forget plot] {2} \closedcycle;

\addplot[mark=none,
  domain=8:14,
  pattern=flexible hatch,
  hatch distance=10pt,
  hatch thickness=0.5pt,
  draw=none,
  pattern color=plotrangecolor,
  area legend,
  forget plot] {2} \closedcycle;


\addplot  coordinates {(1,1) (4,0.4) (7,0.6) (8,0.5)     (14,0.2)};
\addplot  coordinates {(1,0.2)  (3,0.3)  (5,0.7)  (9,1)      (14,0.5)};


  \addlegendentry{value 1}
  \addlegendentry{value 2}



\end{axis}
\end{tikzpicture}

\end{document}
2
  • I was having an error when using forget plot. This works for me: \addplot [black, forget plot, error bars/.cd, y dir=plus, y explicit]. But this doesn't work: \addplot [black, error bars/.cd, y dir=plus, y explicit, forget plot]. Notice the placement of forget plot. The error was Error: I do not know the key '/pgfplots/error bars/forget plot' and I am going to ignore it. I don't quite understand what the problem is, but I'll leave it here in case someone else bumps into the same issue.
    – Daniel
    Feb 20, 2020 at 17:10
  • 1
    @Daniel error bars/.cd is the culprit there. After that all keys are assumed to be in the error bars family, which forget plot is not. Hence the message that /pgfplots/error bars/forget plot is not known, the "path" to forget plot is /pgfplots/forget plot. Feb 20, 2020 at 18:14

You must log in to answer this question.

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