Pgfplots: how to fill the area under a curve with oblique lines (hatching) as a pattern?
You can define your own pattern that can take optional arguments for setting the distance between the lines and the line thickness.
To make sure the axis line stays black, you can set axis on top
, which will draw the axis lines last.
And to get the hatch pattern in the legend as well, add the key area legend
to the plot:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\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}
}
\makeatother
\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[color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\addlegendentry{z}
\addplot+[mark=none,
domain=0:1,
samples=100,
pattern=flexible hatch,
area legend,
pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
\addlegendentry{Interval 1}
\addplot+[mark=none,
domain=-2:-0.5,
samples=100,
pattern=flexible hatch,
hatch distance=5pt,
hatch thickness=0.5pt,
draw=blue,
pattern color=cyan,
area legend]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
\addlegendentry{Interval 2}
\end{axis}
\end{tikzpicture}
\end{document}
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\pgfdeclarepatternformonly{north east lines wide}%
{\pgfqpoint{-1pt}{-1pt}}%
{\pgfqpoint{10pt}{10pt}}%
{\pgfqpoint{9pt}{9pt}}%
{
\pgfsetlinewidth{0.4pt}
\pgfpathmoveto{\pgfqpoint{0pt}{0pt}}
\pgfpathlineto{\pgfqpoint{9.1pt}{9.1pt}}
\pgfusepath{stroke}
}
\begin{axis}[xmin=-4,xmax=4,xlabel={z},ymin=0,ymax=1]
\addplot[color=red,domain=-4:4,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)};
\addlegendentry{z}
\addplot+[mark=none,domain=0:1,samples=100,%
pattern=north east lines wide,%
pattern color=red!50!yellow]%
{1/sqrt(2*pi)*exp(-x^2/2)}
\closedcycle;
\addlegendentry{Interval}
\end{axis}
\end{tikzpicture}
\end{document}
Version 1.10 of pgfplots has been released just recently, and it comes with a new solution for the problem to fill the area between plots.
Note that the old solution is still possible and still valid; this here is merely an update which might simplify the task. In order to keep the knowledge base of this site up-to-date, I present a solution based on the new fillbetween
library here:
\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[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}
This solution re-uses the pattern styles as proposed and written by Jake. However, it simplifies the filling by means of the new features of \usepgfplotslibrary{fillbetween}
.
In this case, we assign the name name path=A
to the input path. Then, we generate an artificial \path
with name B
: this \path
is not drawn and not filled; it resembles the entire x axis.
Finally, we can generate two \addplot fill between
instructions, each with individual styles. The only difference is the soft clip
argument: it allows to restrict the fill path to the given domain
arguments.