0

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

set hatched but not legend

modification from this page

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}
Stefan Pinnow
  • 29,535

1 Answers1

1

You can draw the orange area with the same \addplot as the pattern by adding preaction = {fill = orange} to its optional arguments and remove the separate \addplot for the orange area. This should fix your legend entry.

\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}

\addplot[area legend,preaction={fill=orange},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}

plot

luki
  • 1,796
  • Very simple! Thanks, exactly what I want. – StephaneC Apr 01 '20 at 04:49
  • You're welcome, I am happy I could help! – luki Apr 01 '20 at 11:16
  • If this solution works for you, you can accept the answer by clicking the check mark. ;-) – luki Apr 01 '20 at 12:09
  • Sorry, I forget to clicking on the check mark. Done. – StephaneC Apr 10 '20 at 12:22
  • In my version of LaTex (2015?), sometimes I need "\shorthandoff{:}" before the "\addplot" with "soft clip={domain=-2:-0.5}" because if not, sometimes, not always, I have a message error. I must remember it. Very nice, I am very happy. Best regards – StephaneC Apr 10 '20 at 12:22
  • Another thing, I put a \makeatother command, after Jake's code to hatch the pattern for sure it run well everywhere. Am I right? I think that "ça ne mange pas de pain" it's an collaquial French expression to say that does not involve a great risk by doing that, I have got nothing to lose. – StephaneC Apr 10 '20 at 12:26