Blog

Fick diffusion maps in LaTeX: concentration and flux with PGFPlots

A transdermal patch holds a fixed concentration (C_0) at the skin surface. Across a layer of thickness (L), Fick's laws of diffusion in the steady state give a linear concentration (c(x)=C_0(1-x/L)) and a constant flux (q_x=DC_0/L). Those two fields make a clear pair of colour maps in PGFPlots, and the same axes can show the transient approach to that state. This post works through the LaTeX example from a TeX Stack Exchange question asked on 29 November 2026, and the accepted sequence of maps.

The source is ordinary LaTeX: it compiles with a standard TeX distribution, and it is compatible with Overleaf.

The steady state

The starting figure plots three views of the same one-dimensional problem. Depth (x) runs downwards, from the surface at (0) to (L). The first map is (c(x)/C_0), which falls from 1 to 0. The second is the dimensionless flux (q_x/(DC_0/L)), which is 1 everywhere. A line plot on the right shows the same two profiles against (x/L).

Result

Three panels for steady Fick diffusion through a layer of thickness L. The left colour map, labelled c(x)/C0, runs from red at depth 0 to blue at depth L, with a colour bar from 0 to 1. The middle map, labelled qx/(DC0/L), is a uniform light green, with a colour bar from 0 to 2. The right-hand plot shows a solid blue line falling from 1 to 0 and a dashed red line constant at 1, with legend entries c/C0 and qx/(DC0/L). The depth axis is labelled profondità x.

LaTeX source

\documentclass[tikz,border=4pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\begin{tikzpicture}
\pgfplotsset{mappa/.style={view={0}{90}, width=1.8cm, height=4.5cm, scale only axis,
  xmin=0, xmax=1, ymin=0, ymax=1, y dir=reverse, xtick=\empty,
  ytick={0,0.5,1}, yticklabels={$0$,$L/2$,$L$}, ylabel={profondit\`a $x$},
  colormap/jet, colorbar, domain=0:1, y domain=0:1, samples=2, samples y=40}}

\begin{axis}[mappa, title={$c(x)/C_0$}, point meta min=0, point meta max=1,
  colorbar style={ytick={0,0.5,1}}, name=A]
  \addplot3[surf, shader=interp]{1-y};
\end{axis}

\begin{axis}[mappa, at={(A.east)}, xshift=1.7cm, anchor=west, title={$q_x/(DC_0/L)$},
  point meta min=0, point meta max=2, colorbar style={ytick={0,1,2}},
  ylabel={}, yticklabel=\empty, name=B]
  \addplot3[surf, shader=interp]{1};
\end{axis}

\begin{axis}[at={(B.east)}, xshift=1.9cm, anchor=west, width=3cm, height=4.5cm,
  scale only axis, xmin=0, xmax=1, ymin=0, ymax=1.1, xlabel={$x/L$},
  legend style={font=\scriptsize, at={(0.5,1.04)}, anchor=south}]
  \addplot[blue, very thick, domain=0:1] {1-x};
  \addlegendentry{$c/C_0$}
  \addplot[red, very thick, dashed, domain=0:1] {1};
  \addlegendentry{$q_x/(DC_0/L)$}
\end{axis}
\end{tikzpicture}
\end{document}

Open in Lilia

This example is a complete document. Open it in Lilia to change it and compile it yourself, or download the PDF it produces: no account is needed for that.

Because neither profile depends on time, the maps themselves do not move. To show a process, the same layout can be reused at several values of a dimensionless time.

The transient towards the steady state

Fick's second law, (\partial_t c = D\partial_x^2 c), with (c(0,t)=C_0), (c(L,t)=0) and (c(x,0)=0) for (x>0), has a standard series solution. In the dimensionless time (\tau=Dt/L^2),

\frac{c(x,\tau)}{C_0}
  = 1 - \frac{x}{L}
    - \frac{2}{\pi}\sum_{n=1}^{\infty}
      \frac{1}{n}\sin\frac{n\pi x}{L}\,e^{-n^2\pi^2\tau}

\frac{q_x(x,\tau)}{DC_0/L}
  = 1 + 2\sum_{n=1}^{\infty}
      \cos\frac{n\pi x}{L}\,e^{-n^2\pi^2\tau}

By (\tau\approx 0.5), both series are close to the steady profiles above. Jean Pimenta's accepted answer puts the three axes in a macro, \diffusionframe{...}, substitutes a ten-term truncation of the series (accurate for (\tau\ge 0.03)), and loops over four times with \foreach; standalone writes one frame per page. The example below keeps the answer's functions, styles and axes, but writes the four tikzpicture environments out, each calling \diffusionframe with one value of (\tau): the PDF is the same four pages, and each frame is its own figure when you open the example in Lilia. The flux colour scale and the line-plot height are raised so the early-time peak near (x=0) is not clipped. The Result tab shows one frame at a time: Next page steps through all four.

Result

A four-page sequence of the same three panels at dimensionless times 0.05, 0.12, 0.25 and 0.50. At tau=0.05 the concentration is still high only near the surface and the flux is peaked there, around 2.5, then falls towards L. At tau=0.12 both profiles reach deeper and the flux peak is lower. At tau=0.25 the concentration is approaching a straight line and the flux is flattening towards 1. At tau=0.50 the concentration map and the blue line are the linear steady profile, and the flux map and the dashed red line are essentially constant at 1.
Page 1 of 4
Transient Fick maps at four values of tau: page 2 of 4 (the description of page 1 covers every page).
Page 2 of 4
Transient Fick maps at four values of tau: page 3 of 4 (the description of page 1 covers every page).
Page 3 of 4
Transient Fick maps at four values of tau: page 4 of 4 (the description of page 1 covers every page).
Page 4 of 4
Page 1 of 4

LaTeX source

\documentclass[tikz,border=4pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

% Truncated Fourier series (10 modes, accurate for tau >= 0.03)
\pgfmathdeclarefunction{conc}{2}{%
  \pgfmathparse{%
    1 - #1 - (2/pi)*(%
      sin(deg(1*pi*#1))*exp(-1*pi*pi*#2)/1 +
      sin(deg(2*pi*#1))*exp(-4*pi*pi*#2)/2 +
      sin(deg(3*pi*#1))*exp(-9*pi*pi*#2)/3 +
      sin(deg(4*pi*#1))*exp(-16*pi*pi*#2)/4 +
      sin(deg(5*pi*#1))*exp(-25*pi*pi*#2)/5 +
      sin(deg(6*pi*#1))*exp(-36*pi*pi*#2)/6 +
      sin(deg(7*pi*#1))*exp(-49*pi*pi*#2)/7 +
      sin(deg(8*pi*#1))*exp(-64*pi*pi*#2)/8 +
      sin(deg(9*pi*#1))*exp(-81*pi*pi*#2)/9 +
      sin(deg(10*pi*#1))*exp(-100*pi*pi*#2)/10%
    )%
  }%
}

\pgfmathdeclarefunction{flux}{2}{%
  \pgfmathparse{%
    1 + 2*(%
      cos(deg(1*pi*#1))*exp(-1*pi*pi*#2) +
      cos(deg(2*pi*#1))*exp(-4*pi*pi*#2) +
      cos(deg(3*pi*#1))*exp(-9*pi*pi*#2) +
      cos(deg(4*pi*#1))*exp(-16*pi*pi*#2) +
      cos(deg(5*pi*#1))*exp(-25*pi*pi*#2) +
      cos(deg(6*pi*#1))*exp(-36*pi*pi*#2) +
      cos(deg(7*pi*#1))*exp(-49*pi*pi*#2) +
      cos(deg(8*pi*#1))*exp(-64*pi*pi*#2) +
      cos(deg(9*pi*#1))*exp(-81*pi*pi*#2) +
      cos(deg(10*pi*#1))*exp(-100*pi*pi*#2)%
    )%
  }%
}

\pgfplotsset{
  mappa/.style={
    view={0}{90}, width=1.8cm, height=4.5cm, scale only axis,
    xmin=0, xmax=1, ymin=0, ymax=1, y dir=reverse, xtick=\empty,
    ytick={0,0.5,1}, yticklabels={$0$,$L/2$,$L$}, ylabel={profondit\`a $x$},
    colormap/jet, colorbar, domain=0:1, y domain=0:1, samples=2, samples y=35
  }
}

% One frame: the three axes at dimensionless time #1, drawn inside a tikzpicture.
% The accepted answer puts the tikzpicture in the macro and loops with \foreach;
% writing the four tikzpictures out gives the same four pages.
\newcommand{\diffusionframe}[1]{%
  \begin{axis}[mappa, title={$c(x)/C_0$}, point meta min=0, point meta max=1,
    colorbar style={ytick={0,0.5,1}}, name=A]
    \addplot3[surf, shader=interp]{conc(y,#1)};
  \end{axis}

  \begin{axis}[mappa, at={(A.east)}, xshift=1.7cm, anchor=west,
    title={$q_x/(DC_0/L)$}, point meta min=0, point meta max=2.5,
    colorbar style={ytick={0,1,2}}, ylabel={}, yticklabel=\empty, name=B]
    \addplot3[surf, shader=interp]{flux(y,#1)};
  \end{axis}

  \begin{axis}[at={(B.east)}, xshift=1.9cm, anchor=west, width=3.5cm, height=4.5cm,
    scale only axis, xmin=0, xmax=1, ymin=0, ymax=2.7, xlabel={$x/L$},
    title={$\tau = #1$},
    legend pos=north east, legend style={font=\scriptsize}]
    \addplot[blue, very thick, domain=0:1, samples=40] {conc(x,#1)};
    \addlegendentry{$c/C_0$}
    \addplot[red, very thick, dashed, domain=0:1, samples=40] {flux(x,#1)};
    \addlegendentry{$q_x/(DC_0/L)$}
  \end{axis}%
}

\begin{document}
\begin{tikzpicture}\diffusionframe{0.05}\end{tikzpicture}

\begin{tikzpicture}\diffusionframe{0.12}\end{tikzpicture}

\begin{tikzpicture}\diffusionframe{0.25}\end{tikzpicture}

\begin{tikzpicture}\diffusionframe{0.50}\end{tikzpicture}
\end{document}

Open in Lilia

This example is a complete document. Open it in Lilia to change it and compile it yourself, or download the PDF it produces: no account is needed for that.

The four pages can be converted to a GIF or a video with pdftoppm and a tool such as ffmpeg.

The same macro also works inside the animate package, which embeds the animation in the PDF itself. The accepted answer's animate version, with the example's macro, goes in an article or beamer document: keep the preamble of the example, add \usepackage{animate}, and replace the four pictures with

\begin{center}
\begin{animateinline}[controls, loop, poster=last]{8}
  \multiframe{15}{rTau=0.05+0.03}{%
    \begin{tikzpicture}\diffusionframe{\rTau}\end{tikzpicture}%
  }
\end{animateinline}
\end{center}

With pdflatex this gives one page holding 15 frames, from (\tau=0.05) to (\tau=0.47), with play controls under the figure; poster=last shows the last frame until the animation is played. It took about three minutes to compile in our test, and playing it needs a PDF viewer that runs the file's JavaScript, such as Adobe Acrobat Reader or Okular. The multi-page sequence is the portable form, and it is the one in the example.

Doing it in Lilia

Lilia is a browser editor where a paper is a sequence of blocks with LaTeX underneath. The examples above are complete documents.

Open the example. Use Open and edit in Lilia on the example's tab. You can change (\tau), the number of terms, or the boundary values and compile the PDF there. The same source can also be pasted with Paste LaTeX from the command palette (the ? button in the top bar), or imported as a .tex file or an Overleaf zip from Import / Export.

Check that it compiles. Click Compile above the PDF. Lilia compiles the document with pdflatex, a real TeX engine. Your copy is an ordinary article with the example's title at the top: the stationary maps fit on the first page, and the four transient frames follow one another over three pages. The transient frames take a while to compute (under a minute in our test), longer than the quick check under the shield in the left rail (Validate) waits, so use Compile for that one.

Hand it on. Export → LaTeX gives a single .tex or a project .zip (flat or Overleaf-style). The plot definitions go with you as ordinary LaTeX, so a co-author can compile them on Overleaf or in a local TeX distribution.

Credit

This post follows the question Concentration/flux map sequence for diffusion (Fick's laws): stationary case, asked by Sebastiano on TeX Stack Exchange on 29 November 2026. The transient sequence is from Jean Pimenta's accepted answer. Both are used here under CC BY-SA, with two changes: the stationary figure is set in standalone rather than on a full page, and the four transient frames are written out rather than looped.

Summary

  • In the steady state, (c/C_0) is linear in depth and the dimensionless flux is 1. That is already a complete PGFPlots figure.
  • The transient from an initially empty layer is a Fourier series in (\tau=Dt/L^2). A ten-term truncation is enough for (\tau\ge 0.03).
  • Putting the three axes in a macro and drawing it once per value of (\tau) (in a \foreach loop, or one tikzpicture each) gives a multi-page sequence that approaches the steady maps by (\tau=0.50).

If you'd like to try this without installing anything, Lilia's Free plan includes 3 documents, every export format and the compile check: liliaeditor.com.