TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}

%%
%% 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] {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

share|improve this question
up vote 8 down vote accepted

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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.