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

Skip to content

Update round fix #19260

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 2 commits into from
May 10, 2021
Merged

Update round fix #19260

merged 2 commits into from
May 10, 2021

Conversation

hildogjr
Copy link
Contributor

@hildogjr hildogjr commented Jan 8, 2021

PR Summary

On webpage int(pct/100.*np.sum(allvals)) cause a wrong round and absolute number representation.
Here int(round(foo)) make a not needed int(foo) conversion.
Remove the int(foo).

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

On webpage `int(pct/100.*np.sum(allvals))` cause a wrong round and absolute number representation.
Here `int(round(foo))` make a not needed `int(foo)` conversion.
Remove the `int(foo)`.
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 while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. 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.

@QuLogic
Copy link
Member

QuLogic commented Jan 8, 2021

I can understand if it's not needed, but what do you mean by wrong? Can you give an example?

@hildogjr
Copy link
Contributor Author

Yes. Now the example on the official page use int(pct/100.*np.sum(allvals)) at the perceptual_absolute(foo):

scores = [116, 133, 18, 19, 4, 2]
def perceptual_absolute(pct, allvals):
        absolute = int(pct/100.*np.sum(allvals))
        return '{:.2f}%\n({:d})'.format(pct, absolute)
plt.pie(scores.values(), explode=explode,
                            autopct=lambda pct: perceptual_absolute(pct, list(scores.values())),
                            pctdistance=1.1, labeldistance=1.2, startangle=0)

int(pct/100.*np.sum(allvals)) get 132 but the correct is 133, given by round(pct/100.*np.sum(allvals)).

The actual code here in master (maybe not deployed yet) use int(round(pct/100.*np.sum(allvals))) but round(foo) already returns a int type. The call int(foo) is not used at the code.

@jklymak
Copy link
Member

jklymak commented Jan 11, 2021

For consistency sake with the rest of our examples, it should probably just be np.round and let the formatter take care of making it an integer.

@hildogjr
Copy link
Contributor Author

In this case may be necessary to do int(np.round(foo)) because np.round(foo) returns numpy.float type.

@ianhi
Copy link
Contributor

ianhi commented Jan 11, 2021

because np.round(foo) returns numpy.float type.

This is actually a bug in numpy (numpy/numpy#15297) that is fixed in more recent versions. See numpy/numpy#15840

So if you do pip install --upgrade numpy that behavior should go away.

Edit: woops I didn't read this line from late in the PR:

No, we didn't change np.round, only ndarray.round.

@jklymak
Copy link
Member

jklymak commented Jan 11, 2021

Yeah, fair enough - I didn't realize that '{:d}'.format(np.round(3.4)) fails with ValueError: Unknown format code 'd' for object of type 'float'. print('%d'%np.round(3.4)) works just fine.

@hildogjr
Copy link
Contributor Author

hildogjr commented Jan 12, 2021

Interesting point:

print('%d'% 56)
print('%d'% 56.0)
print('%d'% 56.9)

All print "56".

print('%d'% '-')
print('%d'% 45)

All print "-". The conversion is forced before the string replacement.

I have edit to use int(np.round(foo)) instead round(foo).

@jklymak jklymak merged commit ca8044c into matplotlib:master May 10, 2021
@QuLogic QuLogic added this to the v3.5.0 milestone May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants