Skip to content

Conversation

@ianhi
Copy link
Collaborator

@ianhi ianhi commented Jan 14, 2026

Make the toolbar savefig button respect the matplotlib rcparams

Closes: #138

and thereby

Closes: #234
Closes: #339

Implementation

Let the backend handle the actual buffer generation, then just add the correct mimetype to the bundle for download. Much simpler than i expected.

Tests

The most important test is the manual_test notebook i added. I ran through this and it worked for me as expected. I will try it again with the PR binder link

@github-actions
Copy link
Contributor

Binder 👈 Launch a binder notebook on branch ianhi/ipympl/ian/rcparam

@ianhi
Copy link
Collaborator Author

ianhi commented Jan 14, 2026

@martinRenou if you have time to look at this that would be amazing.

ianhi and others added 3 commits January 15, 2026 02:14
Fixes matplotlib#138, matplotlib#234, matplotlib#339

The Download button now respects ALL matplotlib savefig.* rcParams
instead of always saving as PNG with hardcoded settings.

Implementation:
- Override Toolbar.save_figure() to call canvas._send_save_buffer()
- Add Canvas._send_save_buffer() that calls figure.savefig() without
  hardcoded parameters (respects all rcParams)
- Send buffer + format metadata to frontend via ipywidgets comm
- Update frontend handle_save() to accept buffers from backend
- Support multiple formats with correct MIME types:
  PNG, PDF, SVG, EPS, JPEG, TIFF, PS, TIF
- Set correct file extensions based on format
- Maintain backward compatibility with canvas.toDataURL() fallback

Respects these rcParams:
- savefig.format (png, pdf, svg, jpg, eps, etc.)
- savefig.dpi (resolution)
- savefig.transparent (transparent backgrounds)
- savefig.facecolor (custom background colors)
- All other savefig.* parameters

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Manual test notebook (tests/manual_test_rcparams_save.ipynb):
- Tests all savefig.* rcParams (format, transparent, facecolor, dpi)
- Tests multiple formats: PNG, PDF, SVG, JPEG
- Includes verification checklist
- Documents expected behavior for each test case

Python unit tests (tests/test_download.py):
- Test _send_save_buffer respects savefig.format (PNG, PDF, SVG)
- Test download() method calls _send_save_buffer
- Test Toolbar.save_figure() calls _send_save_buffer
- Test respects savefig.dpi and savefig.transparent rcParams
- Test warns on unsupported format (e.g., webp)
- Add 'tests' to pytest testpaths in pyproject.toml

Addresses issues matplotlib#138, matplotlib#234, matplotlib#339

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
- Add public download() method to Canvas class
- Allows triggering downloads from Python code without clicking button
- Respects all savefig rcParams like the toolbar button
- Includes comprehensive docstring with examples
- Add test file demonstrating programmatic usage

Use cases:
- Batch downloading multiple figures
- Automated workflows
- Custom save logic in notebooks

Example:
    fig, ax = plt.subplots()
    ax.plot([1, 2, 3])
    fig.canvas.download()  # Triggers browser download

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Copy link
Member

@martinRenou martinRenou left a comment

Choose a reason for hiding this comment

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

That's great! Thanks for this

@ianhi ianhi changed the title Ian/rcparam FIX: Toolbar savefig respects rcparams Jan 15, 2026
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.

Download figure as pdf, not as png. Saved figure backgrounds are always transparent Save button should save in rcparams format, not "png"

2 participants