Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

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-actionsgithub-actionsbot 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 uson gitter for real-time discussion.

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

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

Copy link
Member

@rcomerrcomer 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.

brifore13 reacted with thumbs up emoji
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
ContributorAuthor

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.

rcomer reacted with thumbs up emoji
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 ourincubator 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 inzip_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 theValueError 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 theelse on line 2948.

@rcomer
Copy link
Member

The type hint will also need updating

@story645
Copy link
Member

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

@rcomerrcomer 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 runboilerplate.py so thatpyplot.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 thispadding as it may end up an array or list and we don't mind which. It means we replace the originalpadding 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 originalrects.

@rcomerrcomer added this to thev3.11.0 milestoneMar 7, 2025
1. ran boilerplate.py2. padding_array to padding3. removed second rects in unit testing
Copy link
Member

@timhoffmtimhoffm left a comment

Choose a reason for hiding this comment

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

@timhoffmtimhoffm merged commitf730235 intomatplotlib:mainMar 18, 2025
39 of 41 checks passed
@github-project-automationgithub-project-automationbot moved this fromNeeds review toWaiting for author inFirst Time ContributorsMar 18, 2025
@timhoffm
Copy link
Member

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

@QuLogicQuLogic moved this fromWaiting for author toMerged inFirst Time ContributorsMar 18, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@timhoffmtimhoffmtimhoffm approved these changes

@github-actionsgithub-actions[bot]github-actions[bot] left review comments

@rcomerrcomerrcomer approved these changes

Assignees
No one assigned
Projects
Milestone
v3.11.0
Development

Successfully merging this pull request may close these issues.

[ENH]: Allow list of padding values for bar_label
4 participants
@brifore13@rcomer@story645@timhoffm

[8]ページ先頭

©2009-2025 Movatter.jp