Thanks to visit codestin.com
Credit goes to github.com

Skip to content

savefig's pad_inches specifies per border padding #30183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

ttshaw1
Copy link

@ttshaw1 ttshaw1 commented Jun 17, 2025

Added a new padding function to transforms.py, called it in backend_bases, and updated documentation where pad_inches shows up

PR summary

Solution for #11764, allowing pad_inches to be set for each border when saving a figure.

  • Why is this change necessary?
    Difficult to get compact but suitably bounding borders in savefig with extra artists in some cases
  • What problem does it solve?
    E.g. a plot with broken axes made with subfigures and diagonal lines indicating the break is hard to bound correctly. This adds a crude but simple way to modify the bounding box as needed.
  • What is the reasoning for this implementation?
    Seems like the simplest way to do it.

import matplotlib.pyplot as plt
import numpy as np

plt.plot(np.arange(1, 10))
plt.xlabel('Test')
plt.ylabel('Test2')
plt.title('Typical Title')
plt.savefig('test_float_0.png', bbox_inches = 'tight', pad_inches = 0)
plt.savefig('test_tuple_0.png', bbox_inches = 'tight', pad_inches = [0, 0, 0, 0])

plt.savefig('test_float_1.png', bbox_inches = 'tight', pad_inches = 1)
plt.savefig('test_tuple_1.png', bbox_inches = 'tight', pad_inches = [1, 1, 1, 1])
plt.savefig('test_tuple_mixed.png', bbox_inches = 'tight', pad_inches = [0, 2, -0.2, 4])

test_float_0
test_float_1
test_tuple_0
test_tuple_1
test_tuple_mixed

PR checklist

Added a new padding function to transforms.py, called it in backend_bases, and updated documentation where pad_inches shows up
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@ttshaw1
Copy link
Author

ttshaw1 commented Jun 17, 2025

I have
[?] Documentation complies with general and docstring guidelines
as a question mark because I made an effort but I'm not positive I didn't overlook a convention.

Also, I didn't do much error checking. The logic is

                        if isinstance(pad_inches, (int, float)):
                            l_pad = r_pad = b_pad = t_pad = pad_inches
                        elif pad_inches in [None, "layout"]:
                            pad_inches = rcParams['savefig.pad_inches']
                            l_pad = r_pad = b_pad = t_pad = pad_inches
                        else:
                            l_pad = pad_inches[0]
                            r_pad = pad_inches[1]
                            b_pad = pad_inches[2]
                            t_pad = pad_inches[3]

so if someone puts in a 3-tuple or some other weird datatype it's just going to return some default python error. Up to y'all what the best way to cover that sort of thing is.

@ttshaw1 ttshaw1 marked this pull request as ready for review June 17, 2025 18:13
Theodore Shaw added 3 commits June 17, 2025 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs review
Development

Successfully merging this pull request may close these issues.

1 participant