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.
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.
rcomer
left a comment
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
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.
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) |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
rcomer
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
| 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.
| ax = plt.gca() | ||
| rects = ax.bar(xs, heights) |
There was a problem hiding this comment.
| 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
timhoffm
left a comment
There was a problem hiding this comment.
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