Skip to content

Conversation

@lucasb-eyer
Copy link
Contributor

Note: this PR builds on top of my other PRs. Only the last commit is unique, the leading commits will disappear once #73 - #75 are merged and I rebase.

Treat Python-only formatters (FuncFormatter, and formatter subclasses) as export-time only by precomputing tick labels and exporting tickvalues, so mpld3 doesn’t drop custom formatter subclasses.
This unifies FuncFormatter and non-matplotlib Formatter subclasses under a single check, preserving existing d3 behavior for built-in formatters like Date/Log/Scalar.
Adds tests for custom formatters in both exporter copies.

As a disclaimer, as usual, I noticed an issue in my use, and fixed it together with gpt-5.2-codex, manually reviewing and cleaning up the changes.

This is mainly for mpld3, exporting figure-level text objects (such as
fig.suptitle) would never get exported. It looks like figure-level text
objects (and objects in general) was completely missing here. This adds
text objects, I might add more in the future.

The alternative was to put figure text objects into the first axis
object, but that's extremely hacky, so I went for the larger but proper
fix instead.

I did this together with gpt-5.1-codex, not alone. Here is what it has
to say:

- Exporter now emits figure-level text (suptitle + fig.text) via a dedicated draw_figure_text call before crawling axes; figure transforms passed directly to process_transform instead of shoving text into the first axes.
- Renderer API gains a draw_figure_text hook (no-op default in base) so non-mpld3 renderers don’t break; FakeRenderer already implements it.
- Figure JSON now carries a texts array and MPLD3Renderer serializes figure-level text entries with proper coordinates/attrs; tests cover presence/positions of exported figure texts.
line-style was previously ignored for line collections, this fixes it.
Note that I did not add support for offset in line-style, which has not
existed in the first place, and I have no need for it.

There's a corresponding commit/PR in mpld3 coming.

This was done together with gpt-5.1-codex (but I reviewed and edited
a lot), here is what it has to say:

Handle collection dasharrays in exporter

- derive dash arrays from collection linestyles/dashes
- carry dasharrays through export and render_path_collection so hlines/vlines/grid keep their patterns
This will allow to render both major and minor gridlines in mpld3, which
fixes the following issue: mpld3/mpld3#527

As per usual, disclaimer that I co-developed this with gpt-5.1-codex,
having it figure out the issues and give implementation recommendations,
with me testing, verifying, and tidying up the code.

Here's what it has to say, especially wrt the change in API call:

- include minor tick values/length and minor grid style in axis props so minor ticks/grids render in mpld3
- read grid color/linewidth/linestyle from tick kwargs (and rcParams fallback) instead of inspecting gridlines[0], avoiding the get_gridlines(which=…) API that isn’t available on matplotlib 3.10”

Rationale for the kw/rc approach:
`Axis.get_gridlines()` doesn’t accept `which` on matplotlib 3.10, so probing `gridlines[0]` for minor/major fails. Pulling style from the tick keyword dict (which matplotlib populates with `grid_*` fields when you call `ax.grid(...)`) plus
`rcParams` defaults gives the same style without needing `get_gridlines(which=…)`, keeping compatibility and matching user-set grid styles.

(I verified, indeed get_gridlines does not allow specifying which ones - seems like an omission in matplotlib API to me)
Minor ticklabels were missing altogether,
The previous fix to make FuncFormatter work, in
https://github.com/mpld3/mplexporter/pull/67/files
and
mpld3/mpld3@e7fa282
had two issues still: 1) the mpl FuncFormatter API also takes the index as
second argument, which was missing. 2) it exported `tickvalues` only in
the FixedLocator case, so these were missing for non-fixed FuncFormatter
and hence the FuncFormatter codepath, which also requires them to be
present, was never hit with non-fixed locators.

Also add a test, and let tests import `matplotlib` via the
backend-setting mechanism, not only `plt`.

This fixes both. Again, this was figured out and helped with gpt-codex
and my careful review+cleanup. Codex even identified the two original
commits I'm linking above :)
Treat Python-only formatters (FuncFormatter, and formatter subclasses) as export-time only by precomputing tick labels and exporting tickvalues, so mpld3 doesn’t drop custom formatter subclasses.
This unifies FuncFormatter and non-matplotlib Formatter subclasses under a single check, preserving existing d3 behavior for built-in formatters like Date/Log/Scalar.
Adds tests for custom formatters in both exporter copies.

As a disclaimer, as usual, I noticed an issue in my use, and fixed it together with gpt-5.2-codex, manually reviewing and cleaning up the changes.
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