I am writing to ask for your help, please. I would like to add a colour (orange for example) to the background of an initial Pattern (see below). Is there an option to do that: to have a legend that is identical to the new figure? (i.e. with two colours : lines and background)
I use the following code to colour the background of the initial pattern, however I am not able to modify the presentation of the legend. \addlegendentry{Interval 2}
Can you tell me how to get the "interval 2" legend to correspond to the pattern used in the graph?
Many thanks for your time and help. Best regards, Steph
Code with orange background added :
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\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{axis}[
xmin=-4,xmax=4,
xlabel={z},
ymin=0,ymax=1,
axis on top,
legend style={legend cell align=right,legend plot pos=right}]
\addplot[name path=A,color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\addlegendentry{z}
\path[name path=B] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot+[draw,pattern=flexible hatch,pattern color=red]
fill between[
of=A and B,
soft clip={domain=0:1},
];
\addlegendentry{Interval 1}
%%%%%%%%%%%%%%% for orange background
\addplot+[ %forget plot,
fill=orange,%opacity=0.3,
]
fill between[
of=A and B,
soft clip={domain=-2:-0.5},
];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\addplot[area legend,pattern=flexible hatch,pattern color=cyan,draw=blue,hatch distance=5pt, hatch thickness=0.5pt]
fill between[
of=A and B,
soft clip={domain=-2:-0.5},
];
\addlegendentry{Interval 2}
\end{axis}
\end{tikzpicture}
\end{document}

