Skip to content

Let post_process customize the x axis of matplotlib plots - #4086

Merged
quaquel merged 1 commit into
mesa:mainfrom
Darshanpawar7:fix-plot-post-process-order
Nov 14, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
Darshanpawar7:fix-plot-post-process-order

Conversation

@Darshanpawar7

@Darshanpawar7 Darshanpawar7 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Pre-PR checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

This PR fixes an issue where changes made to the x-axis through post_process were being overwritten when using Mesa's default Matplotlib backend.

The problem was caused by PlotMatplotlib applying its default x-axis label and tick locator after running the custom post_process function. Moving the function call after the default axis setup ensures that user customizations are preserved.

Bug / Issue

There is no linked issue for this change. This is a small, self-contained bug fix with a minimal reproducer.

The issue is also present in Mesa's visualization tutorials 8 and 9. Both use post_process to set the x-axis label to "Time Step", but the rendered chart still displays "Step".

The same problem affects custom tick locators. For example, setting MultipleLocator(10) inside post_process gets overwritten by Mesa's default MaxNLocator(integer=True).

Implementation

Updated PlotMatplotlib in mesa/visualization/components/matplotlib_components.py.

The default x-axis label and tick locator are now configured before calling post_process. This allows custom changes made through the callback to remain on the rendered chart.

No changes were needed to the public API, and charts that do not use post_process retain their existing behaviour.

Testing

Added test_plot_matplotlib_post_process_can_change_x_axis to tests/visualization/test_components_matplotlib.py.

The test checks that:

  • The default "Step" label and integer tick locator are preserved when post_process does not modify the x-axis.
  • A custom x-axis label ("Time Step") is retained.
  • A custom MultipleLocator is not overwritten.

Before/after verification:

The reproducer uses Mesa's Boltzmann wealth model and the post_process_lines function from tutorial 8.

Expected results:

X-axis label Tick locator
Before fix Step MaxNLocator
After fix Time Step MultipleLocator

Additional notes

  • Tutorials 8 and 9 should now work as written without requiring any changes.
  • The same behaviour may also be worth considering for the 3.5.x-maintenance branch.
  • AI assistance was used during code investigation, implementation, and test preparation.

PlotMatplotlib called post_process(ax) before it set the x label to
"Step" and the x tick locator to MaxNLocator(integer=True). Any x label
or x tick change made in post_process was therefore overwritten, for
example ax.set_xlabel("Time Step") in tutorials 8 and 9.

Call post_process last, after the defaults, as SpaceMatplotlib and
PlotAltair already do. Plots without post_process look the same.
Copilot AI balanced review requested due to automatic review settings November 13, 2026 13:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added testing Release notes label visualization labels Nov 13, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.0% [-1.7%, -0.3%] 🔵 +2.2% [+1.9%, +2.6%]
BoltzmannWealth large 🔵 +1.5% [+0.6%, +2.6%] 🔵 -1.8% [-3.8%, +0.2%]
Schelling small 🔵 +2.2% [+1.5%, +3.1%] 🔵 +0.8% [-0.3%, +1.9%]
Schelling large 🔵 +0.5% [-0.3%, +1.2%] 🔵 -1.1% [-3.5%, +1.3%]
WolfSheep small 🔵 +2.0% [+1.1%, +3.0%] 🔵 +0.3% [-4.7%, +5.3%]
WolfSheep large 🔵 +2.0% [+0.6%, +3.0%] 🔵 +3.2% [+1.4%, +4.9%]
SugarscapeG1mt small 🔴 +4.8% [+3.9%, +5.7%] 🔵 +0.6% [-1.1%, +2.3%]
SugarscapeG1mt large 🔵 +4.2% [+1.5%, +6.5%] 🔵 +2.7% [-0.1%, +5.4%]
BoidFlockers small 🔵 +1.0% [+0.2%, +1.9%] 🔵 +1.1% [+0.6%, +1.5%]
BoidFlockers large 🔵 +1.4% [+0.4%, +2.5%] 🔵 +1.6% [+0.9%, +2.2%]
MultiLevelAllianceModel small 🔵 -1.8% [-4.1%, +0.3%] 🔵 -0.1% [-0.9%, +0.9%]
MultiLevelAllianceModel large 🔵 +1.8% [-0.5%, +3.6%] 🔵 +0.7% [-0.2%, +1.6%]

@quaquel quaquel added the bug Release notes label label Nov 14, 2026
@quaquel
quaquel merged commit 257aebf into mesa:main Nov 14, 2026
18 of 19 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
#4086)

PlotMatplotlib called post_process(ax) before it set the x label to
"Step" and the x tick locator to MaxNLocator(integer=True). Any x label
or x tick change made in post_process was therefore overwritten, for
example ax.set_xlabel("Time Step") in tutorials 8 and 9.

Call post_process last, after the defaults, as SpaceMatplotlib and
PlotAltair already do. Plots without post_process look the same.

(cherry picked from commit 257aebf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label visualization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants