Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

ENH: Add support for per-label padding in bar_label #29696

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

Merged
merged 6 commits into from
Mar 18, 2025

Conversation

brifore13
Copy link
Contributor

PR summary

PR checklist

Implement support for array-like padding in bar_label.

This allows users to specify different padding values for individual labels by passing an array-like object to the padding parameter.

Updated the docstring to reflect acceptance of float or array-like of correct length.

Ex: ax.bar_label(bars, padding=[3, 3, 3, 15])
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.

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Please also add unit tests to verify that the new behaviour does what you intend.

for bar, err, dat, lbl in itertools.zip_longest(
bars, errs, datavalues, labels
try:
if not isinstance(padding, (str, bytes)) and np.iterable(padding):
Copy link
Member

Choose a reason for hiding this comment

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

I do not think it is necessary to check for string or bytes object here. Currently we assume the user correctly passed a float, so I think in future we can assume they correctly passed a float or iterable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @rcomer, currently making the corrections and will add unit tests. This is my first PR request, so thank you for your help and patience.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @brifore13, if you need any help, feel free to ask questions here. Or you may prefer to ask them in our incubator gitter room.

if len(padding_array) != len(bars):
raise ValueError(
f"padding must be of length {len(bars)} when passed as a sequence")
padding_list = list(padding_array)
Copy link
Member

Choose a reason for hiding this comment

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

The array will work fine in zip_longest, so we do not need to convert this to a list.

else:
# single value, apply to all labels
padding_list = [padding] * len(bars)
except (TypeError, ValueError):
Copy link
Member

Choose a reason for hiding this comment

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

This will catch the ValueError you added for the wrong length iterable. I do not think you meant to do that.

# single value, apply to all labels
padding_list = [padding] * len(bars)
except (TypeError, ValueError):
# not iterable or wrong length, use scalar padding
Copy link
Member

Choose a reason for hiding this comment

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

you already handled the "not iterable" case with the else on line 2948.

@rcomer
Copy link
Member

rcomer commented Mar 2, 2025

The type hint will also need updating

padding: float = ...,

@story645
Copy link
Member

story645 commented Mar 4, 2025

try installing the pre-commit hooks to help spot and fix the pre-commit errors:

https://matplotlib.org/devdocs/devel/development_setup.html#install-pre-commit-hooks

Added unit testing for float and array-like processing. Modified logic based on requested changes. Updated type hint.
Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

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

Thanks @brifore13 I think this is looking in good shape now. I just have some minor comments on the change and you will also need to run boilerplate.py so that pyplot.bar_label gets updated too.

bars, errs, datavalues, labels
if np.iterable(padding):
# if padding iterable, check length
padding_array = np.asarray(padding)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
padding_array = np.asarray(padding)
padding = np.asarray(padding)

I suggest to just call this padding as it may end up an array or list and we don't mind which. It means we replace the original padding but that is OK since we were not going to need the original again.

Comment on lines 8880 to 8881
ax = plt.gca()
rects = ax.bar(xs, heights)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ax = plt.gca()
rects = ax.bar(xs, heights)

For efficiency, we can remove these lines and just reuse the original rects.

@rcomer rcomer added this to the v3.11.0 milestone Mar 7, 2025
1. ran boilerplate.py
2. padding_array to padding
3. removed second rects in unit testing
Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

This should get a What's New entry. See https://matplotlib.org/devdocs/users/next_whats_new/README.html

lib/matplotlib/axes/_axes.py Show resolved Hide resolved
doc/users/next_whats_new/bar_label_padding_update.rst Outdated Show resolved Hide resolved
lib/matplotlib/axes/_axes.py Outdated Show resolved Hide resolved
@timhoffm timhoffm merged commit f730235 into matplotlib:main Mar 18, 2025
39 of 41 checks passed
@github-project-automation github-project-automation bot moved this from Needs review to Waiting for author in First Time Contributors Mar 18, 2025
@timhoffm
Copy link
Member

Thanks @brifore13, and congratulations on your first contribution to Matplotlib! 🎉 We'd welcome to see you back!

@QuLogic QuLogic moved this from Waiting for author to Merged in First Time Contributors Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

[ENH]: Allow list of padding values for bar_label
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.