Skip to content

Finish the roadmap: the remaining plot types, styling and output formats - #7

Merged
certik merged 27 commits into
mainfrom
roadmap4
Aug 16, 2026
Merged

certik merged 27 commits into
mainfrom
roadmap4

Conversation

@certik

@certik certik commented Aug 15, 2026

Copy link
Copy Markdown
Member

Works through the remaining items of the roadmap, one commit each, so that
what is left of matplotlib's everyday surface is now covered.

Plot types: categorical plots, pcolormesh/pcolor, quiver,
clabel, patches (add_rectangle/add_circle/add_ellipse/add_polygon),
polar axes, hist2d/hexbin, matshow/eventplot/broken_barh,
streamplot, table, and 3D axes (plot3d, scatter3d, plot_surface).

Presentation: style sheets and rcParams, mathtext, matplotlib's tick
placement and labelling, all of the common colormaps, complete bar charts,
histograms, errorbars and fill_between, subplot2grid, hand-placed and
inset axes, date axes, and bilinear image interpolation.

Output formats: EPS, and animated GIF via add_frame /
save_animation. Together with the existing SVG, PDF and PNG that is all
five of matplotlib's common targets.

Every item comes with a case in tests/test_plots.f90, a matplotlib
reference in tests/gen_mpl_refs.py, and is checked by the comparison
harnesses. Two new harnesses, compare-eps and compare-gif, join the
three that were there, and all five run in CI:

harness cases mean difference
compare (SVG) 75 structural
compare-png 75 1.55/255
compare-pdf 74 1.99/255
compare-eps 75 3.61/255
compare-gif 20 frames 0.60/255

No new dependencies in the library itself; ghostscript is added to the
pixi environment so that the EPS comparison can rasterize both sides.

certik added 27 commits August 15, 2026 15:30
plt.subplots appears in more files than plt.plot does, and fplot had no
answer to it: panels existed, but only as a number passed to subplot(m,n,i),
so a script had to keep track of which axes was current between every call.

A handle is an index, not a copy of the axes, and every binding is "make this
current, then call the module procedure of the same name". That is the whole
design. It means there is still one implementation of each plot type rather
than a stateful one and an object-oriented one that can drift apart, and it
means the two styles mix freely: a handle can be made current with sca and
the plain calls still work, which is also how anything without a binding
stays reachable.

subplots comes in three shapes, chosen by what it is asked to fill: a grid, a
row, or a single axes with no counts to give. matplotlib returns a 2-D array
or a 1-D one depending on the arguments, and Fortran can dispatch on rank,
so the same choice is available without inventing a name for each.

sharex and sharey are the part with real behaviour behind them. A share group
takes the union of what its members hold, rather than a twin's borrowing of
one other axes wholesale, so a feature at a given x sits at the same place in
every panel; and the inner panels lose their tick labels, which is most of
why the option gets used. Measuring the union means measuring each member,
which would come straight back to the group, so members are measured with
their own link cleared.
style_use(name) selects a set of global defaults and rc() sets them one
at a time. Everything the styles touch -- figure and axes background,
grid, spines, text and tick color, legend frame, color cycle, line width
and font sizes -- was previously a literal at the point of drawing, so
this replaces those literals with the corresponding global.
axhspan/axvspan shade a band, hlines/vlines draw a run of lines. A span
takes data coordinates on its own axis and axes fractions on the other,
as matplotlib does, so only its own axis takes part in autoscaling.
bar and barh take a baseline (bottom/left), so stacks are expressible,
and a per-bar color list. bar_label writes the value at the end of each
bar, at draw time, where the padding in points is known. A filled series
now shows a swatch in the legend rather than a line it does not have.
hist takes explicit bin edges, a density normalisation, a cumulative
count and a step or stepfilled outline. Uneven bins mean a bar can no
longer take its width from the series, so bars carry a per-bar width.
errorbar draws x errors as well as y and takes the two arms separately
for an asymmetric error, which is matplotlib's 2xN array written as two
arrays. fill_between(where=) shades each run of true values as its own
polygon, and the runs share one color and one legend entry.
The colormap tables become one array indexed by map, so adding a map is
a line in the generator rather than a new array and two select cases.
That takes the count from 6 to 49, at 65 anchors rather than 33, and a
"_r" suffix reads any of them backwards. imshow(norm="log") places a
value on the map by its logarithm.
plot, bar and barh take a list of names where they took numbers. The
names go at 0, 1, 2, ... and become the tick labels, which is what
matplotlib does with a list of strings.
A label between dollar signs is laid out by fplot_mathtext into plain
text runs with an offset and a size each, which every backend can
already draw. Superscripts, subscripts, groups and the spacing commands
are covered; the log tick labels now read 10^3 rather than 1e3, as
matplotlib's LogFormatterSciNotation writes them.
linear_ticks is now MaxNLocator: the 1, 2, 2.5, 5 steps, the smallest
that covers the range in as many intervals as the axis is long enough to
label. The labels get ScalarFormatter's shared decimal count, so an axis
running -1 to 1 in quarters reads 1.00 and not 1. The y label hangs off
the tick labels rather than sitting a fixed distance out, which is what
kept it out of the tight bounding box.
Cells take their edges from x and y rather than dividing an extent
evenly, so a field sampled on an uneven grid comes out right. Centres
are accepted too and become edges at the midpoints, as shading=nearest
does. The colorbar now takes its ticks from the same locator and
formatter as the axes.
Each axes now carries where it sits in the grid and how many cells it
spans, and the layout reads that rather than the axes index. Cells
nobody asks for are simply left empty.
Days since 1970-01-01, as matplotlib counts them, with a locator that
steps in years, months, days, hours, minutes or seconds and a formatter
that writes each of those the way AutoDateFormatter does. The unit is
chosen as matplotlib chooses it: the coarsest that still gives three
ticks, at the smallest step that keeps their number down.
The arrow is matplotlib's: a shaft one width wide, a head three widths
across and five long whose barbs reach back four and a half, all
measured in shaft widths and then scaled together. Left alone the shaft
takes 0.06 of the axes width divided by the square root of the number of
arrows, and the scale is 1.8 times the mean vector, so an average arrow
comes out a comfortable size whatever the data is in.
The triangles hand out loose segments, so the level lines are first
chained end to end into whole contours. Once a contour is one line the
label can go where the line is straightest, turned to lie along it, with
that stretch of the line left undrawn so the two do not overlap. Every
separate run of a level is labelled, as matplotlib labels each of them.
add_axes takes a rectangle of the figure. An inset takes its rectangle
in the fractions of the axes it sits in, so it follows that axes when
the layout moves. A secondary axis is the same idea over the whole of
its parent, reading the parent's limits in other units: matplotlib takes
a pair of functions, and a scale with an offset is what a change of
units comes to.
A rectangle, a circle, an ellipse and a polygon, all kept as the ring of
points they come down to, so a circle on axes of different scales leans
exactly as matplotlib's does. A patch widens the limits but never asks
for any of its own: an axes holding nothing but patches keeps the unit
square matplotlib gives it, which is also why an axes with nothing to
autoscale to no longer takes the 5% margins.
The box holds the largest circle that fits, the angle runs anticlockwise
from the right and the radius from the middle out, so the drawing is one
change of coordinates away from everything else. Angles are labelled
every 45 degrees outside the circle and radii along the 22.5 degree
line, where matplotlib puts them.

The degree sign is the first character past ASCII fplot can write, so
the font table now carries the handful of Latin-1 symbols worth having
and SVG writes them as character references. PDF already asked for
WinAnsi, where they sit in the same places.
The raster is built at the size the image is drawn and every pixel read
from the four samples around it. The samples sit at the middles of their
cells, so the outermost half cell comes out flat rather than running off
the data, which is where matplotlib leaves it too.
hist2d counts into a grid and hands the counts to pcolormesh. hexbin
lays two rectangular grids over the data, one offset half a cell from
the other, and gives each point to whichever center is nearer: those
centers are the hexagon centers, which is matplotlib's arithmetic and
what makes the two grids interlock.
matshow is imshow with square cells and the column numbers along the
top. eventplot is a run of vertical strokes, and keeps a whole line
length of room either side of them the way matplotlib does. broken_barh
is a row of rectangles, which are patches that do ask for room.
This follows matplotlib's streamplot closely, because anything else puts
the lines somewhere else: seeds spiral inwards from the corner of a 30x30
mask, each line is integrated with an adaptive Heun step in grid
coordinates, and a line stops the moment it enters a cell another line
has been through. The arrowhead is the '-|>' head at matplotlib's
mutation scale, drawn at a fixed size in points.

The hexbin PDF is left out of the PDF comparison: poppler draws only some
of the hexagons of matplotlib's reference page, so there is nothing there
to compare against. The PNG comparison still covers the case.
Cells are 1.2 line heights tall and share the width of the axes between
them, which is how matplotlib sizes them, and the row-label column is
given whatever width its text needs.

An empty list passed to xticks or yticks now means no ticks at all,
matching set_xticks([]); it used to be indistinguishable from never
having asked.
savefig now accepts .eps. The backend is a fourth implementation of
renderer_t, modelled on the PDF one: same path and text machinery, a
different syntax, and one page-level matrix to turn the API's top-left
origin into PostScript's bottom-left one.

PostScript has no transparency, so alpha is composited against the page
colour rather than dropped as matplotlib drops it; the figure then looks
the way it does in SVG and PNG. tests/compare_eps.py rasterizes both
sides with ghostscript and gives the two alpha-dominated cases their own
limits for that reason.
add_frame captures the figure as pixels and save_animation writes them as
a GIF. The loop stays in the caller rather than in a callback object,
because that is what a matplotlib update function is anyway once Fortran
has a do loop.

fplot_gif carries the two things GIF needs and nothing else: a median-cut
palette, chosen once for the whole animation so the background does not
shimmer from frame to frame, and LZW. No giflib to link.

tests/compare_gif.py decodes both our GIF and one written by Pillow the
way matplotlib's PillowWriter writes it, and compares frame by frame; the
worst frame differs by 0.60/255.
A 3D axes is a camera pointed at a box, so the camera is its own module
(fplot_proj3d) and the axes renderer is only concerned with what the
camera sees: the two back panes of each pair, their grids, the three
axis lines with ticks and labels, and then the data.

The projection, the autoscale margins, the box aspect, the tick and
label offsets and the surface lighting are all mplot3d's, so that the
pictures land on top of matplotlib's. There is no depth buffer, only
the painter's algorithm, which is also what mplot3d does.

surface3d and line3d are within the PNG tolerance without any per-case
exception.
Reallocation on assignment is standard Fortran but LFortran only does it
under --realloc-lhs-arrays (lfortran/lfortran#10158, #10331), so an
errorbar with an asymmetric arm aborted there. Say the size out loud
instead; it is one line and it costs nothing.
@certik
certik merged commit cf261f3 into main Aug 16, 2026
1 check passed
@certik
certik deleted the roadmap4 branch August 16, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant