Your Fortran code can solve differential equations, simulate the evolution of the universe and the quantum realm. Surely it can draw a decent plot? Let’s say goodbye to slow and fragile glue scripts! Let performance and style meet with fplotlib.
@hkvzjal and I would like to announce fplotlib, a pure Fortran plotting
library now hosted under fortran-lang:
- Repository: GitHub - fortran-lang/fplotlib: Fortran plotting library · GitHub
- Documentation & Gallery: https://fplotlib.fortran-lang.org/
fplotlib uses the familiar Matplotlib-style API, adapted to Fortran, and
tests our plots against Matplotlib in CI to ensure pixel-perfect agreement
(except for text and math rendering where we improved the quality, see below).
We support 2D and 3D plotting, subplots, colormaps, contours, vector fields,
statistics, and animations; we have all the usual backends (SVG, PNG,
PDF, EPS, GIF, and APNG) and no external dependencies—everything is
self-contained in the pure Fortran source files, including the default fonts
and a TeX-style typesetting engine for high-quality math and text rendering.
The library should work with any Fortran compiler (tested with GFortran, Flang,
LFortran and ifx).
use fplotlib
call plot(x, sin(x), "b-", lw=2.0_dp, label="$\sin(x)$")
call plot(x, cos(x), "r--", lw=2.0_dp, label="$\cos(x)$")
call title("Trigonometric functions")
call xlabel("$x$")
call ylabel("$y$")
call grid(.true.)
call legend()
call savefig("out.svg") ! or "out.png", "out.pdf", "out.eps"
Plotting & Animations in Pure Fortran
fplotlib supports plot, scatter, bar/barh, hist/hist2d/hexbin, boxplot/violinplot, fill_between, errorbar, contour/contourf, imshow/pcolormesh, quiver/streamplot, polar, 3D plot_surface/plot_wireframe/trisurf/quiver3d/bar3d, subplots/gridspec/subplot_mosaic, constrained_layout, and frame-by-frame animations (add_frame + save_animation to GIF or APNG).
Here are a few examples from the live gallery, generated directly by Fortran programs:
Wave Propagation Animation (GIF)

Rotating 3D Möbius Strip (Animated PNG / APNG)

Multi-Panel Colormapped Images with Shared Colorbar
What Makes fplotlib Unique
1. Pure Fortran, Zero External Dependencies
fplotlib is a self-contained, pure Fortran fpm package; it does not
depend on C, Python, external programs like LaTeX, or external files like fonts.
- Built-in vector backends (SVG, PDF with embedded Type 3 fonts and
ActualText, EPS), built-in antialiased PNG rasterizer, and built-in GIF / APNG encoders. - Embedded Lato (regular, bold, italic) and Lete Sans Math (with full OpenType
MATHtable metrics and kerning), so every format renders identically on every platform with high-quality math rendering. - Tested in CI across GFortran, Flang, Intel
ifx, and LFortran, including AddressSanitizer/UBSan and a 171-plot visual regression suite checked against Matplotlib.
2. Native Fortran Documentation Galleries (sphinx-fplotlib-gallery)
The repository includes sphinx-fplotlib-gallery, a Sphinx extension that
integrates with Sphinx-Gallery so any
Fortran / fpm project can build, run, and embed native Fortran plot examples
(including animated GIFs and APNGs) right inside its Sphinx documentation. This
is analogous to how Python packages build Matplotlib galleries.
Each gallery example is a standalone .f90 program with ! comment prose and ! %% block separators. sphinx-fplotlib-gallery builds all examples with fpm, runs each in a scratch directory, and embeds every saved figure under the code block that produced it:
# docs/conf.py
extensions = [
"sphinx_gallery.gen_gallery",
"fplotlib_gallery",
]
sphinx_gallery_conf = {
"examples_dirs": ["gallery"],
"gallery_dirs": ["auto_gallery"],
"filename_pattern": r"[\\/]plot_",
"image_scrapers": ("fplotlib_gallery.fplotlib_scraper",),
"reset_modules": (),
"inspect_global_variables": False,
}
Our own documentation at Gallery — fplotlib 0.1.0 is built this way.
3. Built-in TeX Math & Knuth–Plass Typesetting Inside Plots
Modern plots on webpages and in journal articles typically use sans-serif fonts
and Matplotlib uses sans-serif fonts by default for the same reason. In order
to make the math formulas look nice, they should also use sans-serif fonts, to
be consistent with the rest of the plot.
Furthermore, the highest-quality result can only be
achieved by implementing the full TeX algorithm for both text and math, and by
using high-quality compatible sans-serif fonts for both the text (the Lato font
for regular, bold and italic) and math (Lete Sans Math with full OpenType MATH
table metrics and kerning). We embed the glyphs of the fonts and the metrics
directly as Fortran source code to ensure everything just works on every
platform and with every backend. We use a very efficient storage scheme, so the
source file with the font data is only 150 KB, and it is not the largest source
file in fplotlib. We embed all math and all European Latin characters including
all accents (if you find anything missing, please let us know).
fplotlib includes a pure-Fortran TeX math layout engine (checked against 93
XeTeX reference formulas and 29 Typst delimiter references) and a
Knuth–Plass paragraph typesetter with Liang hyphenation (checked against
XeTeX line-breaking references to within 0.07 bp).
With textbox(), plots can embed full MyST Markdown blocks—mixing bold, italic, inline and displayed TeX equations ($...$, $$...$$), bullet lists, smart quotes/dashes, and justified hyphenated paragraphs wrapped to the exact axes or card width.
Below are two examples showing how to embed high-quality paragraphs with equations in the plot, captions, or descriptions. Notice how everything is cleanly aligned, with even spacing, no unnatural gaps between lines, and text and math flowing naturally together. It is equivalent to what LaTeX produces with the same sans-serif fonts for both text and math. We spent a lot of effort ensuring everything is done right.
Planck’s Blackbody Radiation with a Typeset Derivation Card & Justified Caption
Aerodynamic Flow Regime Map with Wrapped Callout Boxes
4. Scientific Reports Directly from Fortran
Given that producing high-quality plots required us to implement a full
TeX-style typesetting engine with vector graphics and multiple backends, we also
exposed this core to be able to emit multi-page MyST Markdown reports (PDF, SVG,
PNG, EPS) with YAML front matter, cross-references ({numref}, {ref},
{eq}), booktabs tables, :::{literalinclude}, custom templates, and embedded
vector plots captured in memory—either via typeset / typeset_file / fpm run typeset, or via the immediate-mode report API (markdown, include_figure,
include_table, save):
call markdown("# A Damped Oscillator" // achar(10) // "The model follows $y(t) = e^{-\alpha t}\cos(\omega_0 t)$.")
call figure(figsize=[5.6_dp, 2.45_dp])
call plot(t, y, "C0-", lw=1.8_dp)
call include_figure(caption="Damped harmonic motion $y(t)$.", label="fig:motion", width="84%")
call clf()
call include_table(results, columns=[character(len=20) :: "$t$ (s)", "$y(t)$", "$e^{-\alpha t}$", "$E(t)/E_0$"], &
caption="Sampled displacement and energy.", label="tab:values")
call markdown("As shown in {numref}`fig:motion` and {numref}`tab:values`, ...")
call save("hpc_report.pdf")
We think this will be very handy for generating reports directly from computational HPC codes, with no dependencies other than pure Fortran.
Our goal with fplotlib is to just focus on comprehensive plotting, but only
simple reports. However one can create a full typesetting system (like LaTeX or
Typst) by using fplotlib as a dependency for the actual text, math and plots
typesetting and layout of paragraphs, pages, tables, etc., and the system built
on top can just focus on implementing the frontend: parsing user files, handling
templates, bibliography, various integrations, etc.
5. Re-entrant, Owned Graphics Core
Under the hood, the layout and rendering engines use explicit contexts
(math_context_t, typeset_context_t, md_document_t, report_t) and produce
owned frame_t / paged_document_t values that can be composed, transformed,
clipped, and exported to files or in-memory byte buffers (render_svg,
render_pdf, render_png, render_report) for Jupyter notebooks and web
hosts.
Getting Started
Add fplotlib to your fpm.toml:
[dependencies]
fplotlib = { git = "https://github.com/fortran-lang/fplotlib" }
Browse the full gallery of 2D/3D plots, animations, rich-text insets, and typeset documents at Gallery — fplotlib 0.1.0.
Feedback, bug reports, gallery examples, and contributions are welcome at GitHub - fortran-lang/fplotlib: Fortran plotting library · GitHub!




