# Pgfplots: how to fill the area under a curve with oblique lines (hatching) as a pattern?

I want to hatch the area under a curve (graph of a function) with oblique lines on an interval (to illustrate the function's integral on that interval). This can be done elegantly in gnuplot...

...but I simply cannot reproduce this with pgfplots. The only solution I managed to find with google is the use of the patterns tikz library, however it is unacceptibly ugly:

I'm not aware of any way to alter the color, line width and - especially - the distance between the hatching lines when patterns library is employed. Is there any way to set it? (So that I can change it to resemble the gnuplot version.)

Or, should I choose a completely different approach to reproduce the gnuplot-style hatching...?

Also note that

• pgfplots changes the color of the x-axis to red on the interval (which is not a critical problem, of course, but not elegant).
• the legend for the filling is much more logical in case of gnuplot. Can I reproduce it with pgfplots?

Here is the code I currently use:

\begin{figure}[ht!]
\centering
\begin{tikzpicture}
\begin{axis}[xmin=-4,xmax=4,xlabel={z},ymin=0,ymax=1]
\addplot[color=red,fill=red, pattern=north east lines,  domain=0:1,samples=100] {1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
\end{axis}
\end{tikzpicture}
\end{figure}

• You say you can do this with gnuplot? Please would you post how? I have been totally unable to find out! Thanks! – user49826 Apr 13 '14 at 17:13
• Please do not post comments or questions as an answer to a question. – Alexander Apr 13 '14 at 17:19

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}]
domain=0:1,
samples=100,
pattern=flexible hatch,
area legend,
pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
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;
\end{axis}
\end{tikzpicture}
\end{document}

• Thank you very much! Works perfectly, you addressed every problem! Just one quick (and very minor) additional question, if your time permits: is there any way to change the order within legend, so that is also identical to the gnuplot-style? I.e. text first (flushed to the right), and then (on the right side) the line/area. – Tamas Ferenci Sep 24 '11 at 16:06
• Your alignment question should be possible by means of legend style={legend cell align=right,legend plot pos=right}. – Christian Feuersänger Sep 24 '11 at 17:36
• @Christian: Yup, that does it! – Jake Sep 24 '11 at 23:53
• Exactly. Works as a wonder; now we have covered every aspect of this question. Thank you all again! – Tamas Ferenci Sep 25 '11 at 8:34
• Just for the record: If you have more than one flexible hatch you are not able to change the color any more. A solution to this problem is e.g. given at tex.stackexchange.com/a/405505/95441 – Stefan Pinnow Dec 11 '17 at 18:34
\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]
pattern=north east lines wide,%
pattern color=red!50!yellow]%
{1/sqrt(2*pi)*exp(-x^2/2)}
\closedcycle;
\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}]

\path[name path=B] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

fill between[
of=A and B,
soft clip={domain=0: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},
];
\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.

• The pattern covers the tick markings on the axis. Is there a way to put the pattern in the bottom layer, so that the tick marks show? – MunHo Oct 2 '15 at 22:37
• I suppose the most simple way to achieve that effect is to use axis on top. – Christian Feuersänger Oct 3 '15 at 9:09
• Just for the record: If you have more than one flexible hatch you are not able to change the color any more. A solution to this problem is e.g. given at tex.stackexchange.com/a/405505/95441 – Stefan Pinnow Dec 11 '17 at 18:34