LabCanvas

Normal distribution with shaded rejection regions in pgfplots, explained

Quick answer

This plot shows the standard normal density with the two-sided 5% rejection regions of a hypothesis test: the tails beyond ±1.96 are shaded red (area 0.025 each) and the middle 95% green. The curve is a pgfplots \addplot of a function declared with \pgfmathdeclarefunction, while each shaded region is a TikZ plot of the same curve, closed down to the axis and cut to size with \clip inside a scope. The legend, the arrows and the labels are drawn by hand at axis coordinates.

Contents

The figure and its code

A bell curve f(x) = 1/sqrt(2π) e^(−x²/2) in red on centred axes from −4 to 4, the area between −1.96 and 1.96 shaded light green and labelled Area = 0.95, the two tails beyond ±1.96 shaded red, the right one labelled Area = 0.025 and 1.96 marked with an arrow, the peak value 0.4 marked, and a small legend Fail to reject H0 and Reject H0.
Normal distribution: rejection regions, typeset with TeX Live 2026 (pdfLaTeX) from the code below, exactly as it is in xinychen/awesome-latex-drawing.
Copyright (c) 2019 Xinyu Chen · MIT licence (full text). The licence asks that this notice stay with the code: keep the first lines when you reuse it.
% Normal distribution: rejection regions
% Source: https://github.com/xinychen/awesome-latex-drawing/blob/a11dbbb3796fb279790509fb87e0083f8d11fc96/pgfplots-function/normal_pdf_3.tex
% Copyright (c) 2019 Xinyu Chen. MIT licence: https://github.com/xinychen/awesome-latex-drawing/blob/master/LICENSE
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\usepackage[scaled]{helvet}
\usepackage[T1]{fontenc}
\renewcommand\familydefault{\sfdefault}
\usepackage[eulergreek]{sansmath}
\pgfplotsset{
tick label style = {font=\sansmath\sffamily}}

\pgfkeys{/pgf/number format/fixed, /pgf/number format/precision=5, /pgf/number format/.cd, 1000 sep = {}}
\pgfplotsset{compat=newest}

\begin{document}

\pgfmathdeclarefunction{gauss}{2}{\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\begin{tikzpicture}

\begin{axis}[
    width=10cm, % Set the width of the axis
    height=6cm, % Set the height of the axis
    scaled ticks=false,
    axis lines=center,
    enlargelimits=upper,
    line width=0.8,
    xlabel = {$x$},
    ylabel = {$f(x)$},
    yticklabel = \empty,
    xmin = -5, xmax = 4.5,
    ymin = -0.05, ymax = 0.45,
    every axis x label/.style={at={(ticklabel* cs:1.02)}, anchor=west,}, every axis y label/.style={at={(ticklabel* cs:1.02)}, anchor=south}
]

    \begin{scope}
        \clip (1.96,0) rectangle (4,1);
        \fill[red!40, opacity=0.6] plot[domain=-4:4, smooth] (\x, {1/sqrt(2*pi)*exp(-\x*\x/2)}) -- (4, 0) -- (-4, 0) -- cycle;
    \end{scope}

    \begin{scope}
        \clip (-1.96,0) rectangle (-4,1);
        \fill[red!40, opacity=0.6] plot[domain=-4:4, smooth] (\x, {1/sqrt(2*pi)*exp(-\x*\x/2)}) -- (-4, 0) -- (4, 0) -- cycle;
    \end{scope}

    \begin{scope}
        \clip (-1.96,0) rectangle (1.96,0.4);
        \fill[green!20, opacity=0.6] plot[domain=-1.96:1.96, smooth] (\x, {1/sqrt(2*pi)*exp(-\x*\x/2)}) -- (1.96, 0.1) -- (1.96, 0) -- (-1.96, 0) -- (-1.96, 0.1) -- cycle;
    \end{scope}

\draw[draw=gray!60, line width=0.1, fill=green!20] (1.3,0.38) rectangle (1.8,0.40);
\draw[right] (2,0.39) node {\scriptsize\color{gray}Fail to reject $H_0$};
\draw[draw=gray!60, line width=0.1, fill=red!40] (1.3,0.32+0.01) rectangle (1.8,0.34+0.01);
\draw[right] (2,0.33+0.01) node {\scriptsize\color{gray}Reject $H_0$};

\addplot + [smooth, mark=none, domain=-4:4, samples=100, color=red, line width=1.5] {gauss(0,1)};

\node at (-2.8,0.3) {\color{red!90!black}$f(x)=\frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2}$};

% \node at (3,0.45) {\small\color{gray}$95\%$ confidence interval};

% \node at (3,0.35) {\small\color{gray}$\mu\pm \underbrace{1.96}_{\text{two-tailed}}\sigma$};

\draw[arrows=<-, thick, color=red!70] (1.96,0.02)--(1.96,0.13);
\node at (2,0.16) {\footnotesize\color{red!90!black}$1.96$};

\draw[arrows=<-, thick, color=red!70] (2.2,0.015)--(3,0.075);
\node at (3.4,0.1) {\footnotesize\color{red!90!black}Area = $0.025$};

\draw[arrows=<-, thick, color=green!70] (-1.2,0.1)--(-2,0.15);
\node at (-2.6,0.18) {\footnotesize\color{green!70!black}Area = $0.95$};

\node at (-0.5,0.4) {0.4};

\end{axis}
\end{tikzpicture}

\end{document}

Sans-serif fonts and number formatting

Lines 5–16 of the code

Most of the preamble sets the look. \usepackage[scaled]{helvet} with \renewcommand\familydefault{\sfdefault} makes Helvetica the text font, and sansmath provides \sansmath, used in tick label style so that the tick numbers are sans-serif too (with eulergreek for Greek letters). The \pgfkeys line sets the number format to fixed point with up to five decimals and no thousands separator. compat=newest asks for the newest pgfplots behaviour, which among other things makes axis coordinates the default for TikZ drawing inside the axis, something the rest of the file relies on.

A Gaussian as a pgfmath function

Lines 20–21 of the code

\pgfmathdeclarefunction{gauss}{2}{…} adds a function with two arguments, the mean (#1) and the standard deviation (#2), to the maths parser. Its body computes the normal density 1/(σ√(2π)) exp(−(x−μ)²/(2σ²)) and leaves the result in \pgfmathresult, as declared functions must. Note the x in the body: it is the variable pgfplots defines while it samples an \addplot, so gauss(0,1) works there. The TikZ plot commands further down use \x instead, which is why they write the formula out again rather than calling gauss.

Centred axes and labels at the ends

Lines 24–37 of the code

axis lines=center draws the axes through the origin, and xmin = -5, xmax = 4.5 with ymin = -0.05, ymax = 0.45 fix the window; enlargelimits=upper adds a little room at the upper ends. yticklabel = \empty keeps the y ticks but prints no numbers; the one value that matters, 0.4, is placed by hand at the end of the file. scaled ticks=false stops pgfplots from factoring a common power of ten out of the labels. The axis labels are moved with every axis x label/.style={at={(ticklabel* cs:1.02)}, anchor=west,}: ticklabel* cs measures along the axis from 0 at its start to 1 at its end, so 1.02 is just past the arrow tip, and anchor=west puts the label to the right of that point.

Shading a region: plot, close, clip

Lines 39–52 of the code

Each shaded region is the same trick. Inside a scope, \clip (1.96,0) rectangle (4,1); restricts all drawing to the strip right of 1.96. Then \fill[red!40, opacity=0.6] plot[domain=-4:4, smooth] (\x, {1/sqrt(2*pi)*exp(-\x*\x/2)}) -- (4, 0) -- (-4, 0) -- cycle; fills the whole area under the curve, closed along the x axis, and the clip keeps only its right tail. The scope ends the clip, so the next region starts unclipped. These coordinates are axis coordinates (1.96 means x = 1.96, not 1.96 cm) because, with a recent compat, axis coordinates are the default inside an axis.

The green middle region is drawn the same way with its own domain, −1.96 to 1.96. Its path takes two detours, (1.96, 0.1) and (-1.96, 0.1), straight up and back down at the edges; they enclose no area, and the clip rectangle up to 0.4 cuts nothing because the curve peaks at 0.399. Both could be dropped. The modern alternative to this clip-and-fill pattern is the fillbetween library, used in the variations.

A legend drawn by hand

Lines 54–57 of the code

pgfplots’ \legend only knows \addplot commands, and the shaded regions are plain TikZ paths, so the legend is two small rectangles and two text nodes at axis coordinates: (1.3,0.38) rectangle (1.8,0.40) in the green fill, the label at (2,0.39), and the same in red just below. In \draw[right] (2,0.39) node {…} the right option is inherited by the node, which is anchored on its west side. The text is \scriptsize and grey.

The curve and its annotations

Lines 59–76 of the code

The red curve comes last so that it lies on top of the fills: \addplot + [smooth, mark=none, domain=-4:4, samples=100, color=red, line width=1.5] {gauss(0,1)};. The + appends these options to the plot’s default style from the cycle list instead of replacing it. The formula is a node at (−2.8, 0.3). Each annotation is an arrow drawn with arrows=<-, so the tip is at the first coordinate, the point being pointed at, plus a text node near its other end: 1.96 on the axis, Area = 0.025 in the right tail and Area = 0.95 in the middle. Two commented-out nodes (lines 63 and 65) show labels the author tried and dropped. The last node writes 0.4 beside the peak, standing in for the suppressed y tick labels.

Techniques used

Variations

Our own code, written for this page and typeset with TeX Live 2026 like the figure above. Each one is a complete example: copy it, or open it in the TikZ editor and change it. This code is CC0 1.0, free to use without conditions.

The same regions with fillbetween and a real legend

The fillbetween library does what the clip-and-fill scopes do, from named paths: name path=f on the curve, name path=base on an invisible {0}, then fill between[of=f and base, soft clip={domain=1.96:4}] for each region. Because the fills are \addplots, \legend can list them (area legend draws a filled box), and the visible curve is plotted last so that it lies on top. The x ticks include ±1.96.

A standard normal curve on left-hand axes from −4 to 4 with ticks at −3, −1.96, 0, 1.96 and 3, the area between −1.96 and 1.96 shaded green and both tails shaded red, and a legend: fail to reject H0 (95 %), reject H0 (5 %), standard normal.
the 95% region and the 5% rejection regions with fillbetween, the legend made by pgfplots
Preamble: \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{fillbetween} \pgfplotsset{compat=1.18}
\begin{tikzpicture}
  \begin{axis}[
      width=10cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.45,
      domain=-4:4, samples=101, no markers, enlarge x limits=false,
      xtick={-3,-1.96,0,1.96,3}, ytick={0,0.1,...,0.4},
      xlabel={$z$}, ylabel={$\varphi(z)$},
      legend style={draw=none, font=\small, at={(0.5,-0.3)}, anchor=north, legend columns=-1,
                    /tikz/every even column/.append style={column sep=4mm}},
    ]
    % named paths for fill between: the curve and the axis (both invisible here)
    \addplot[name path=f, draw=none, forget plot] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[name path=base, draw=none, forget plot] {0};
    \addplot[fill=green!25, area legend] fill between[of=f and base, soft clip={domain=-1.96:1.96}];
    \addplot[fill=red!45, area legend] fill between[of=f and base, soft clip={domain=1.96:4}];
    \addplot[fill=red!45, forget plot] fill between[of=f and base, soft clip={domain=-4:-1.96}];
    % the curve last, so it is drawn on top of the fills
    \addplot[very thick, black] {exp(-x^2/2)/sqrt(2*pi)};
    \legend{fail to reject $H_0$ (95\,\%), reject $H_0$ (5\,\%), standard normal}
  \end{axis}
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      width=10cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.45,
      domain=-4:4, samples=101, no markers, enlarge x limits=false,
      xtick={-3,-1.96,0,1.96,3}, ytick={0,0.1,...,0.4},
      xlabel={$z$}, ylabel={$\varphi(z)$},
      legend style={draw=none, font=\small, at={(0.5,-0.3)}, anchor=north, legend columns=-1,
                    /tikz/every even column/.append style={column sep=4mm}},
    ]
    % named paths for fill between: the curve and the axis (both invisible here)
    \addplot[name path=f, draw=none, forget plot] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[name path=base, draw=none, forget plot] {0};
    \addplot[fill=green!25, area legend] fill between[of=f and base, soft clip={domain=-1.96:1.96}];
    \addplot[fill=red!45, area legend] fill between[of=f and base, soft clip={domain=1.96:4}];
    \addplot[fill=red!45, forget plot] fill between[of=f and base, soft clip={domain=-4:-1.96}];
    % the curve last, so it is drawn on top of the fills
    \addplot[very thick, black] {exp(-x^2/2)/sqrt(2*pi)};
    \legend{fail to reject $H_0$ (95\,\%), reject $H_0$ (5\,\%), standard normal}
  \end{axis}
\end{tikzpicture}
\end{document}

Type I and type II errors

The next figure in most statistics courses: the null distribution H₀ and an alternative H₁, a one-sided critical value at 1.645 (α = 0.05), α shaded under H₀ beyond it and β under H₁ before it. Both shaded areas use fill between with a soft clip on either side of the critical value, and xticklabels replaces the tick numbers with labels.

Two bell curves, a solid H0 centred at 0 and a dashed H1 centred at mu 1, a vertical line labelled critical value at 1.645, the right tail of H0 beyond it shaded red and marked alpha, and the part of H1 left of it shaded blue and marked beta.
two normal curves H₀ and H₁ with the critical value, α (red) and β (blue)
Preamble: \usepackage{tikz} \usepackage{pgfplots} \usepgfplotslibrary{fillbetween} \pgfplotsset{compat=1.18}
\begin{tikzpicture}
  \begin{axis}[
      width=11cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.5,
      domain=-3.5:6, samples=121, no markers, enlarge x limits=false,
      xtick={0,1.645,2.8}, xticklabels={$0$, $1.645$, $\mu_1$}, ytick=\empty, xlabel={$z$},
    ]
    % H0: mean 0; H1: mean 2.8; one-sided test at alpha = 0.05 (critical value 1.645)
    \addplot[name path=h0, draw=none] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[name path=h1, draw=none] {exp(-(x-2.8)^2/2)/sqrt(2*pi)};
    \addplot[name path=base, draw=none] {0};
    \addplot[fill=red!45] fill between[of=h0 and base, soft clip={domain=1.645:6}];
    \addplot[fill=blue!30] fill between[of=h1 and base, soft clip={domain=-3.5:1.645}];
    \addplot[very thick, black] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[very thick, black, dashed] {exp(-(x-2.8)^2/2)/sqrt(2*pi)};
    \draw[thick] (1.645,0) -- (1.645,0.43) node[above, font=\small] {critical value};
    \node[font=\small] at (-1.6,0.3) {$H_0$};
    \node[font=\small] at (4.5,0.3) {$H_1$};
    \node[font=\footnotesize, red!60!black] at (2.15,0.013) {$\alpha$};
    \node[font=\footnotesize, blue!60!black] at (1.1,0.04) {$\beta$};
  \end{axis}
\end{tikzpicture}
Open in the TikZ editor
Full document
A complete file you can compile as it is.
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      width=11cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.5,
      domain=-3.5:6, samples=121, no markers, enlarge x limits=false,
      xtick={0,1.645,2.8}, xticklabels={$0$, $1.645$, $\mu_1$}, ytick=\empty, xlabel={$z$},
    ]
    % H0: mean 0; H1: mean 2.8; one-sided test at alpha = 0.05 (critical value 1.645)
    \addplot[name path=h0, draw=none] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[name path=h1, draw=none] {exp(-(x-2.8)^2/2)/sqrt(2*pi)};
    \addplot[name path=base, draw=none] {0};
    \addplot[fill=red!45] fill between[of=h0 and base, soft clip={domain=1.645:6}];
    \addplot[fill=blue!30] fill between[of=h1 and base, soft clip={domain=-3.5:1.645}];
    \addplot[very thick, black] {exp(-x^2/2)/sqrt(2*pi)};
    \addplot[very thick, black, dashed] {exp(-(x-2.8)^2/2)/sqrt(2*pi)};
    \draw[thick] (1.645,0) -- (1.645,0.43) node[above, font=\small] {critical value};
    \node[font=\small] at (-1.6,0.3) {$H_0$};
    \node[font=\small] at (4.5,0.3) {$H_1$};
    \node[font=\footnotesize, red!60!black] at (2.15,0.013) {$\alpha$};
    \node[font=\footnotesize, blue!60!black] at (1.1,0.04) {$\beta$};
  \end{axis}
\end{tikzpicture}
\end{document}

Frequently asked questions

How do I shade the area under a normal curve in pgfplots?

Load \usepgfplotslibrary{fillbetween}, give the curve name path=f and an invisible \addplot[name path=axis, draw=none] {0}; a second name, then \addplot[fill=red!40] fill between[of=f and axis, soft clip={domain=1.96:4}];. Without the library, fill the area under the whole curve inside a scope with a \clip rectangle, as this figure does.

Why can I write (1.96,0) inside a pgfplots axis?

Since compat=1.11, TikZ coordinates inside an axis are axis coordinates by default, so (1.96,0) means x = 1.96, y = 0. With an older compat setting you would write (axis cs:1.96,0).

Why does my declared gauss function not work in a TikZ plot?

A function whose body uses x only works inside \addplot, where pgfplots defines x. A TikZ plot uses the variable \x; either pass it as an argument, gauss(\x,0,1) with three parameters, or write the expression out.

References

  1. xinychen/awesome-latex-drawing at commit a11dbbb: pgfplots-function/normal_pdf_3.tex (MIT licence)github.com
  2. pgfplots manual, §4.17.1 Accessing Axis Coordinates in Graphical Elements (axis cs is the default)tikz.dev
  3. pgfplots manual, §4.9.1 Placement of Axis Descriptions (ticklabel* cs)tikz.dev
  4. pgfplots manual, §4.9.9 Axis Lines (axis lines=center)tikz.dev
  5. pgfplots manual, §4.3.3 Computing Coordinates with Mathematical Expressionstikz.dev
  6. pgfplots manual, §5.7 Fill between (name path, soft clip)tikz.dev
  7. PGF/TikZ manual, §15.9 Clipping and Fading (Soft Clipping)tikz.dev
  8. PGF/TikZ manual, §96 Customizing the Mathematical Engine (\pgfmathdeclarefunction)tikz.dev
  9. sansmath package on CTANctan.org

The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Xinyu Chen, under the MIT licence; the variations on this page are ours, CC0 1.0.

TikZ guides for these techniques

More figures explained

Change this figure in your browserOpen the figure or any variation in the free TikZ editor: it is typeset with TeX Live as you type, with errors pointed at their line.

Open the figure in the TikZ editor