-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
ENH: Add support for per-label padding in bar_label #29696
Conversation
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])
There was a problem hiding this 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.
There was a problem hiding this 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.
lib/matplotlib/axes/_axes.py
Outdated
for bar, err, dat, lbl in itertools.zip_longest( | ||
bars, errs, datavalues, labels | ||
try: | ||
if not isinstance(padding, (str, bytes)) and np.iterable(padding): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
lib/matplotlib/axes/_axes.py
Outdated
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) |
There was a problem hiding this comment.
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.
lib/matplotlib/axes/_axes.py
Outdated
else: | ||
# single value, apply to all labels | ||
padding_list = [padding] * len(bars) | ||
except (TypeError, ValueError): |
There was a problem hiding this comment.
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.
lib/matplotlib/axes/_axes.py
Outdated
# single value, apply to all labels | ||
padding_list = [padding] * len(bars) | ||
except (TypeError, ValueError): | ||
# not iterable or wrong length, use scalar padding |
There was a problem hiding this comment.
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.
The type hint will also need updating matplotlib/lib/matplotlib/axes/_axes.pyi Line 271 in 03b74ea
|
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.
There was a problem hiding this 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.
lib/matplotlib/axes/_axes.py
Outdated
bars, errs, datavalues, labels | ||
if np.iterable(padding): | ||
# if padding iterable, check length | ||
padding_array = np.asarray(padding) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
lib/matplotlib/tests/test_axes.py
Outdated
ax = plt.gca() | ||
rects = ax.bar(xs, heights) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ax = plt.gca() | |
rects = ax.bar(xs, heights) |
For efficiency, we can remove these lines and just reuse the original rects
.
1. ran boilerplate.py 2. padding_array to padding 3. removed second rects in unit testing
There was a problem hiding this 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
Thanks @brifore13, and congratulations on your first contribution to Matplotlib! 🎉 We'd welcome to see you back! |
PR summary
PR checklist