Example: Filling an area below a curve with a line pattern

Published 2014-03-15 | Author: Jake

We would like to fill areas below a curve with different line patterns. We define our 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, we can set axis on top, which will draw the axis lines last.

And to get the hatch pattern in the legend as well, we add the key area legend to the plot.

This code was written by Jake on TeX.SE.

Download as: [PDF] [TEX]  •  [Open in Overleaf]

Filling an area below a curve with a line pattern

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

        hatch distance/.store in=\hatchdistance,
        hatch distance=10pt,
        hatch thickness/.store in=\hatchthickness,
        hatch thickness=2pt
    \pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{flexible hatch}

        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)};
        pattern=flexible hatch,
        area legend,
        pattern color=red]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;
    \addlegendentry{Interval 1}
        pattern=flexible hatch,
        hatch distance=5pt,
        hatch thickness=0.5pt,
        pattern color=cyan,
        area legend]{1/sqrt(2*pi)*exp(-x^2/2)} \closedcycle;    
        \addlegendentry{Interval 2}


Adding comments is currently not enabled.