Maxwell–Boltzmann speed distribution in pgfplots, explained
Quick answer
This plot shows the Maxwell–Boltzmann distribution of molecular speeds at 100 K, 300 K and 1000 K: the curve flattens and its peak moves to higher speeds as the temperature rises. It is a pgfplots axis with three \addplot commands, each evaluating the same formula, written once as a TeX macro that takes the temperature as its argument. The constants are tiny (10−23 and 10−27), which works because pgfplots evaluates plot expressions with a floating-point unit, and the axis label and legend use siunitx for the units.
Contents
The figure and its code

% Maxwell–Boltzmann speed distribution
% Source: https://github.com/janosh/diagrams/blob/7a79b3114e201fad9b6785e936eab859d1bc8c12/assets/maxwell-boltzmann-distribution/maxwell-boltzmann-distribution.tex
% Copyright (c) 2021 Janosh Riebesell. MIT licence: https://github.com/janosh/diagrams/blob/main/license
% The licence’s copyright and permission notice: https://www.labcanvas.com/tikz-library/credits.html
\documentclass{standalone}
\usepackage{pgfplots,siunitx}
\pgfplotsset{compat=newest}
\def\kB{1.38e-23} % Boltzmann constant
\def\mu{1.66e-27} % unified atomic mass unit/Dalton (symbols: m_u or Da)
\def\maxwellboltzmann#1{4 * pi * (\mu / (2 * pi * \kB * #1))^(3/2) * x^2 * exp(-\mu * x^2 / (2 * \kB * #1))}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain = 0:8000,
xlabel = {$v$ [\si{\metre\per\second}]},
ylabel = $P(v)$,
smooth, thick,
axis lines = left,
every tick/.style = {thick},
]
\addplot[color=red,samples=100]{\maxwellboltzmann{100}};
\addplot[color=yellow]{\maxwellboltzmann{300}};
\addplot[color=blue]{\maxwellboltzmann{1000}};
\legend{\SI{100}{\kelvin}, \SI{300}{\kelvin}, \SI{1000}{\kelvin}}
\end{axis}
\end{tikzpicture}
\end{document}
pgfplots, siunitx and compat=newest
Lines 5–8 of the code
The plot needs two packages: pgfplots, which loads TikZ and provides the axis environment, and siunitx for the units. \pgfplotsset{compat=newest} asks for the newest behaviour of whichever pgfplots version compiles the file. That is convenient but means the plot can change after an update; a fixed value such as compat=1.18, as in the variations, keeps the output the same.
Constants and the formula as a macro
Lines 10–13 of the code
The Boltzmann constant and the mass are stored as plain text with \def: \def\kB{1.38e-23} and \def\mu{1.66e-27}, the atomic mass unit in kilograms. The curves are therefore for particles of mass 1 u, roughly hydrogen atoms. Note that \mu is also the Greek letter μ: after line 11, $\mu$ anywhere in this document would print 1.66e-27. A name such as \massu avoids that.
\def\maxwellboltzmann#1{…} is a macro with one parameter, the temperature, which expands into a pgfplots expression in x: 4π (m / 2πkBT)3/2 x² exp(−m x² / 2kBT), the Maxwell–Boltzmann density of speeds. Because the macro is expanded before pgfplots reads the expression, \maxwellboltzmann{100} is the formula with T = 100 written in. Numbers of order 10−27 would overflow TikZ’s ordinary arithmetic, which stops at about ±16384, but \addplot expressions are evaluated with pgfplots’ floating-point unit, which handles them.
The axis
Lines 17–26 of the code
The options of the axis environment apply to every plot in it. domain = 0:8000 sets the speeds that are sampled, smooth joins the samples with a curve instead of straight segments, and thick sets the line width. axis lines = left draws only the left and bottom axes, with arrow tips, instead of a full box, and every tick/.style = {thick} thickens the tick marks to match. The x label uses \si{\metre\per\second}, which siunitx prints as m s−1. pgfplots chooses the y ticks itself and, because the values are small, factors out the common 10−4 at the top of the axis.
Three curves and a legend
Lines 28–34 of the code
Each \addplot evaluates the macro for one temperature. Only the first sets samples=100; the other two use the default of 25 samples, which is enough for the wide curves but would show corners on the narrow 100 K peak, the reason for the extra samples there. The colours are given explicitly: red, yellow and blue. Yellow on white is hard to read, on screen and especially in print, so the first variation uses a colour-blind-safe set with different dash patterns.
\legend{…} lists the entries in plot order, here with siunitx’s \SI{100}{\kelvin}. \SI and \si are the names from siunitx version 2; version 3 still accepts them, and calls them \qty and \unit.
Techniques used
- Function plots with pgfplots
\addplotof an expression inxover adomain, withsamplesandsmooth. - Units with siunitx
\si{\metre\per\second}in the label and\SI{100}{\kelvin}in the legend. - Readable colours why yellow on white fails, and a colour-blind-safe cycle list instead.
- Physics diagrams distributions and other physics plots drawn with TikZ and pgfplots.
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.
Readable colours, siunitx 3 and the most probable speed
The same three curves, written to avoid the tiny constants: with the most probable speed vp = √(2kBT/m), the density is f(v) = 4/√π · v²/vp³ · exp(−v²/vp²), declared once with \pgfmathdeclarefunction{mb}{2}{…} and called as mb(x, 1289.5). A cycle list gives each curve a colour-blind-safe colour and its own dash pattern, the legend uses \qty from siunitx 3, and thin grey lines mark each vp, added with forget plot so they stay out of the legend.

\usepackage{tikz} \usepackage{pgfplots} \usepackage{siunitx} \pgfplotsset{compat=1.18}\begin{tikzpicture}
% f(v) written with the most probable speed vp = sqrt(2kT/m): no tiny constants
\pgfmathdeclarefunction{mb}{2}{\pgfmathparse{4/sqrt(pi)*#1^2/#2^3*exp(-(#1/#2)^2)}}
\begin{axis}[
width=9cm, height=6cm, axis lines=left, ymin=0, ymax=7.8e-4,
xtick={0,2000,...,8000},
domain=0:8000, samples=120, smooth, no markers,
xlabel={speed $v$ / \unit{\metre\per\second}},
ylabel={$f(v)$ / \unit{\second\per\metre}},
cycle list={{blue!70!black, very thick}, {orange!85!black, very thick, dashed},
{teal, very thick, dash dot}},
legend cell align=left,
]
% vp for m = 1 u: 1289.5, 2233.5 and 4077.9 m/s at 100, 300 and 1000 K
\addplot {mb(x, 1289.5)}; \addlegendentry{\qty{100}{\kelvin}}
\addplot {mb(x, 2233.5)}; \addlegendentry{\qty{300}{\kelvin}}
\addplot {mb(x, 4077.9)}; \addlegendentry{\qty{1000}{\kelvin}}
% the most probable speed of each curve, where f(vp) = 4/(e sqrt(pi) vp)
\addplot[gray, thin, forget plot] coordinates {(1289.5,0) (1289.5,6.438e-4)};
\addplot[gray, thin, forget plot] coordinates {(2233.5,0) (2233.5,3.717e-4)};
\addplot[gray, thin, forget plot] coordinates {(4077.9,0) (4077.9,2.036e-4)};
\node[font=\small, anchor=south west] at (axis cs:1289.5,6.438e-4) {$v_p = \sqrt{2k_BT/m}$};
\end{axis}
\end{tikzpicture}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{siunitx}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
% f(v) written with the most probable speed vp = sqrt(2kT/m): no tiny constants
\pgfmathdeclarefunction{mb}{2}{\pgfmathparse{4/sqrt(pi)*#1^2/#2^3*exp(-(#1/#2)^2)}}
\begin{axis}[
width=9cm, height=6cm, axis lines=left, ymin=0, ymax=7.8e-4,
xtick={0,2000,...,8000},
domain=0:8000, samples=120, smooth, no markers,
xlabel={speed $v$ / \unit{\metre\per\second}},
ylabel={$f(v)$ / \unit{\second\per\metre}},
cycle list={{blue!70!black, very thick}, {orange!85!black, very thick, dashed},
{teal, very thick, dash dot}},
legend cell align=left,
]
% vp for m = 1 u: 1289.5, 2233.5 and 4077.9 m/s at 100, 300 and 1000 K
\addplot {mb(x, 1289.5)}; \addlegendentry{\qty{100}{\kelvin}}
\addplot {mb(x, 2233.5)}; \addlegendentry{\qty{300}{\kelvin}}
\addplot {mb(x, 4077.9)}; \addlegendentry{\qty{1000}{\kelvin}}
% the most probable speed of each curve, where f(vp) = 4/(e sqrt(pi) vp)
\addplot[gray, thin, forget plot] coordinates {(1289.5,0) (1289.5,6.438e-4)};
\addplot[gray, thin, forget plot] coordinates {(2233.5,0) (2233.5,3.717e-4)};
\addplot[gray, thin, forget plot] coordinates {(4077.9,0) (4077.9,2.036e-4)};
\node[font=\small, anchor=south west] at (axis cs:1289.5,6.438e-4) {$v_p = \sqrt{2k_BT/m}$};
\end{axis}
\end{tikzpicture}
\end{document}One curve for every temperature
Measured in units of vp, all temperatures give the same curve, 4/√π · u² e−u². The shaded tail, an \addplot over domain=2:3.5 closed down to the axis with \closedcycle and filled, is the share of particles faster than 2vp: erfc 2 + (4/√π) e−4 ≈ 4.6%. The filled plot is drawn first so that the curve lies on top of it.

\usepackage{tikz} \usepackage{pgfplots} \pgfplotsset{compat=1.18}\begin{tikzpicture}
\begin{axis}[
width=8.5cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.95,
domain=0:3.5, samples=100, smooth, no markers, enlarge x limits=false,
xlabel={$u = v/v_p$}, ylabel={$v_p\,f(v)$},
]
% in units of vp every temperature gives the same curve
\addplot[draw=none, fill=orange!40, domain=2:3.5] {4/sqrt(pi)*x^2*exp(-x^2)} \closedcycle;
\addplot[very thick, blue!70!black] {4/sqrt(pi)*x^2*exp(-x^2)};
\draw[dashed] (axis cs:1,0) -- (axis cs:1,{4/sqrt(pi)*exp(-1)});
\node[font=\small, anchor=west] at (axis cs:1.08,0.84) {most probable speed, $u = 1$};
\node[font=\small, anchor=south east, align=left] at (axis cs:3.45,0.22)
{4.6\,\% of the particles\\are faster than $2v_p$};
\end{axis}
\end{tikzpicture}
Full document
\documentclass[border=4pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8.5cm, height=5.5cm, axis lines=left, ymin=0, ymax=0.95,
domain=0:3.5, samples=100, smooth, no markers, enlarge x limits=false,
xlabel={$u = v/v_p$}, ylabel={$v_p\,f(v)$},
]
% in units of vp every temperature gives the same curve
\addplot[draw=none, fill=orange!40, domain=2:3.5] {4/sqrt(pi)*x^2*exp(-x^2)} \closedcycle;
\addplot[very thick, blue!70!black] {4/sqrt(pi)*x^2*exp(-x^2)};
\draw[dashed] (axis cs:1,0) -- (axis cs:1,{4/sqrt(pi)*exp(-1)});
\node[font=\small, anchor=west] at (axis cs:1.08,0.84) {most probable speed, $u = 1$};
\node[font=\small, anchor=south east, align=left] at (axis cs:3.45,0.22)
{4.6\,\% of the particles\\are faster than $2v_p$};
\end{axis}
\end{tikzpicture}
\end{document}Frequently asked questions
How do I plot the Maxwell–Boltzmann distribution in LaTeX?
Use pgfplots: \addplot[domain=0:8000, samples=100, smooth] {4*pi*(m/(2*pi*k*T))^(3/2)*x^2*exp(-m*x^2/(2*k*T))}; with the constants written in or stored in macros. pgfplots evaluates the expression with its floating-point unit, so constants like 1.38e-23 are fine.
Why does TikZ give “Dimension too large” for my formula?
TikZ’s own arithmetic (in \draw plot or coordinates) works with numbers up to about 16383. Plot with pgfplots, whose \addplot uses a floating-point unit, or rescale the formula so the numbers stay small, for example in units of the most probable speed.
Why does $\mu$ print a number in this document?
Line 11 redefines \mu as the atomic mass unit for the formula. Any later $\mu$ in the same document expands to 1.66e-27. Give the constant another name, such as \massu.
References
- janosh/diagrams at commit 7a79b31: assets/maxwell-boltzmann-distribution/maxwell-boltzmann-distribution.tex (MIT licence)github.com
- pgfplots manual, §4.3.3 Computing Coordinates with Mathematical Expressions (domain, samples, the floating-point unit)tikz.dev
- pgfplots manual, §4.5.2 Smooth Plotstikz.dev
- pgfplots manual, §4.5.11 Closing Plots (Filling the Area Under Plots)tikz.dev
- pgfplots manual, §4.7.7 Cycle Liststikz.dev
- pgfplots manual, §4.9.4 Legends and §4.9.5 Legend Appearancetikz.dev
- pgfplots manual (v1.18.3), §2.2 Upgrade remarks (the compat key)mirrors.ctan.org
- PGF/TikZ manual, §96 Customizing the Mathematical Engine (\pgfmathdeclarefunction)tikz.dev
- siunitx package on CTAN (v3): \unit, \qty and the version 2 names \si and \SIctan.org
The walkthrough was checked against these sources and the pictures are the code typeset with TeX Live 2026. The original figure is © Janosh Riebesell and contributors, under the MIT licence; the variations on this page are ours, CC0 1.0.
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