· via Hacker News – Front Page (native)
fplotlib brings a matplotlib-style API to pure Fortran with no external graphics library
A new Fortran plotting library called fplotlib has appeared on Hacker News's front page. It mirrors matplotlib's pylab API and writes SVG, PNG, PDF and EPS with a rasterizer and fonts bundled inside the library.
A plotting library written entirely in Fortran reached the Hacker News front page on 28 November. The project, fplotlib, is hosted under the fortran-lang organization on GitHub and gives Fortran code an interface modeled on matplotlib's pylab: the sequence plot, title, xlabel, grid, legend, savefig will be immediately familiar to anyone who has scripted figures in Python. According to the project's GitHub page, it writes SVG, PNG, PDF and EPS, and it does so without any external graphics library — the PNG rasterizer and the fonts it draws with are part of fplotlib itself.
An API matplotlib users will recognize
The repository's example shows the stateful style working almost call for call against pylab, with format strings like "k-", the bgrcmykw single-letter colors and the C0–C9 references behaving as they do in matplotlib. A show() call renders inline in Jupyter when the kernel is LFortran, and falls back to writing an SVG file otherwise. The file extension selects the output format, and a dpi argument sets the raster size. Convenience keyword arguments let a single plot() call carry the title, axis labels, limits, grid, legend and output filename, and subplot(m, n, i) with a figure-level suptitle() handles multi-panel figures.
Alongside the global interface there is an object-oriented layer: figure_t and axes_t handles allow figures to be managed explicitly and several independent figures to stay open at once. The README notes that everything available through the stateful calls also exists as a method on a handle, and subplots() can return an axes array so a call like axs(1,2)%plot(x, y) mixes with the global style. When axes share a scale, the group's limits cover the union of the data and inner tick labels are dropped, matching matplotlib's behavior.
Breadth of coverage
The feature list reads like a matplotlib inventory. Series types include line, scatter, bar, histogram, errorbar, fill_between, step, stem, barh, pie, boxplot and violinplot, plus streamplot for vector fields, hexbin and hist2d for binned counting, and spy, matshow, eventplot and broken_barh. imshow takes RGB or RGBA arrays, colormaps, extents and colorbars; contour and contourf handle level sets. There are 3D axes with plot_surface and plot_wireframe, and polar axes with angular and radial grids. Six colormaps ship: viridis, plasma, inferno, magma, gray and coolwarm.
Annotation goes beyond simple labels: text and annotate support arrows, rotation and background boxes, and a textbox routine renders a MyST Markdown block with bold, italic, inline and displayed math, lists, and line breaking and hyphenation via the Knuth–Plass algorithm. save_animation() writes animated GIFs or animated PNGs.
Layout machinery mirrors matplotlib as well — tight_layout, constrained_layout, twinx and twiny, inset and secondary axes, and a legend that settles in the emptiest region of the plot when placement is left unset. NaN and infinity values break lines and are excluded from autoscaling, and the drawing order follows matplotlib's, with images under patches, the grid above the patches, lines above that and text on top, plus set_zorder to lift an artist out of the default order.
Where Fortran forces differences
A few departures from matplotlib are dictated by the language rather than chosen. scatter() takes per-point sizes and color-mapped values through two separate keywords, sizes= and cvals=, because a single Fortran dummy argument cannot be overloaded to accept either a scalar or an array the way Python's flexible s= and c= keyword arguments do. Dotted hatch patterns are not drawn, leaving hatching limited to /, , |, -, + and x. And constrained_layout keeps one margin for the entire grid rather than computing row by row as matplotlib does.
Why it matters
Fortran remains a working language for numerical simulation, but producing a publication figure from Fortran code has usually meant exporting the data and plotting it somewhere else, or binding to a C graphics stack. A library that draws those figures directly from the simulation, with an API that scientists already know from matplotlib, removes a genuine step from that workflow, and doing it without any graphics dependency keeps builds simple on the HPC clusters where Fortran typically runs. The documentation and a gallery of examples are online at fplotlib.fortran-lang.org, and the breadth of the documented feature set suggests a project aimed at real scientific plotting rather than a proof of concept. Whether rendering quality and performance hold up under heavy use is the question early adopters from the Hacker News thread will now be answering.
- #fortran
- #plotting
- #matplotlib
- #open-source
- #scientific-computing