Skip to content
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: Added a PercentFormatter class to matplotlib.ticker #6251

Merged
merged 1 commit into from Apr 8, 2016

Conversation

madphysicist
Copy link
Contributor

This is suitable for formatting axes labels as percentages. It has some nice features like being able to convert from arbitrary data scales to percents, a customizable percent symbol and either automatic or manual control over the decimal points.

The original motivation came from my own work as well as these two Stack Overflow questions:

@madphysicist
Copy link
Contributor Author

@mdboom While I realize that you have a script to do this, it is really disconcerting to see your name pop up fractions of a second after I create a PR :)

suitable for use with single-letter representations of powers of
1000. For example, 'Hz' or 'm'.

`places` is the percision with which to display the number,
Copy link
Member

Choose a reason for hiding this comment

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

precision

@madphysicist
Copy link
Contributor Author

I am not familiar enough with Python 2.7 to immediately understand why the following is not working:

s = '{x:0.{decimals}f}'.format(x=x, decimals=decimals)

Are nested formats not allowed?

already scaled to be percentages, `mx` will be 100. Another common
situation is where `max` is 1.0.
"""
def __init__(self, mx=100, decimals=None, symbol='%'):
Copy link
Member

Choose a reason for hiding this comment

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

I think people will rarely want to specify max, so I would put it after the decimals kwarg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I disagree. The most common parameter to modify would be max. This is intended for data going from 0 to 1, or as in the case of the SO question, from 0 to 5. I would expect the decimals argument to generally remain None assuming I have done a decent job with the automated version.

@efiring
Copy link
Member

efiring commented Mar 30, 2016

There are so many cleanup changes here that I recommend breaking this into two PRs: one for the cleanups, and a second one for the new feature.

@madphysicist
Copy link
Contributor Author

No problem with that. Is there an easy way to do that without closing this PR?

@@ -1016,7 +1016,7 @@ def __init__(self, unit="", places=None):
suitable for use with single-letter representations of powers of
1000. For example, 'Hz' or 'm'.

`places` is the percision with which to display the number,
`places` is the prrcision with which to display the number,
Copy link
Member

Choose a reason for hiding this comment

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

Oops!

@QuLogic
Copy link
Member

QuLogic commented Mar 30, 2016

@mdboom While I realize that you have a script to do this, it is really disconcerting to see your name pop up fractions of a second after I create a PR :)

That's https://waffle.io/

No problem with that. Is there an easy way to do that without closing this PR?

Create a new branch starting on this one, rebase it to include only cleanup, and open a new PR for it. When that's merged, rebase this one on top of master and drop all the cleanup from here.

@efiring
Copy link
Member

efiring commented Mar 30, 2016

I think you would make a separate branch on your repo for the cleanups, and make a PR from that. Then, on your pctFormatter branch, isolate the PercentFormatter part with additional commits, and use 'git rebase -i' to squash it down to a single commit. Force-push that to your github repo, and it will replace the present commits in the present PR.

@madphysicist
Copy link
Contributor Author

I did the split (#6253) and fixed the Py2.7 error. Turns out Py3 automatically converts acceptable format precisions into an int. Py2 does not.

The only potentially unrelated change I kept here was moving __all__ to the top.

@madphysicist madphysicist changed the title Added a PercentFormatter class to matplotlib.ticker ENH: Added a PercentFormatter class to matplotlib.ticker Mar 31, 2016
@madphysicist madphysicist changed the title ENH: Added a PercentFormatter class to matplotlib.ticker ENH: Added a PercentFormatter class to matplotlib.ticker Mar 31, 2016
@madphysicist madphysicist force-pushed the pctFormatter branch 3 times, most recently from 94a8859 to fbb8eb4 Compare March 31, 2016 06:23
@madphysicist
Copy link
Contributor Author

Metabolized the docs of EngFormatter.format_eng from #6253 into this commit to get rid of sphinx errors.

@madphysicist madphysicist force-pushed the pctFormatter branch 2 times, most recently from c91a87b to f123047 Compare March 31, 2016 17:32
@madphysicist
Copy link
Contributor Author

I think that this PR is complete (ready for further review). Same goes for #6253.

The failure in Appveyor is the sporadic image comparison failure. It is not related to the code I added as far as I can tell.

@madphysicist
Copy link
Contributor Author

Squawk.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Apr 7, 2016
@mdboom
Copy link
Member

mdboom commented Apr 7, 2016

Cc: @efiring.

@efiring
Copy link
Member

efiring commented Apr 7, 2016

In reading the new code, I find that I waste a lot of time trying to figure out what d really is. I am comfortable with short variable names like x when their meaning is obvious from the context; but d is not obvious here--especially since some places it is in unscaled data units, and other places in percent. So maybe use dspan or xspan, and pcspan, or something like that? And maybe xmax instead of mx? Readability matters, both in the tests and in the active code.

madphysicist added a commit to madphysicist/matplotlib that referenced this pull request Apr 8, 2016
@madphysicist
Copy link
Contributor Author

I changed max to xmax throughout, and changed d to display_range and scaled_range as appropriate. I think that does make a big improvement to readability.

On a side-note, I got d from OldScalarFormatter, originally thinking to reuse its pprint method. I don't think that d needs to be changed at this point, but it is a possibility. d also appears in at least one of the Locators.

(42, None, '^^Foobar$$', 21, 12, '50.0^^Foobar$$'),
)
for xmax, decimals, symbol, x, display_range, expected in test_cases:
yield _percent_format_helper, xmax, decimals, symbol, x, display_range, expected
Copy link
Member

Choose a reason for hiding this comment

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

pep-8 failure here: line too long. You could use parentheses; or you could use

    for case in test_cases:
        yield (_percent_format_helper,) + case

I haven't tested, but I think that would work and would be more readable.
Maybe I would use args and test_args instead of case and test_cases.

@efiring
Copy link
Member

efiring commented Apr 8, 2016

In addition to the pep-8 failure noted above there is a real failure that I have not tracked down (probably you will spot it instantly). Once those are taken care of, I think this will be functionally ready. It's a nice contribution. You could neaten it up by rebasing to squash the commits.

@madphysicist
Copy link
Contributor Author

I went with

for case in test_cases:
    yield (_percent_format_helper, *case)

and also fixed the other stupid typo. Will push with squash soon.

@madphysicist
Copy link
Contributor Author

And now I know why I should have listened the first time.

@madphysicist
Copy link
Contributor Author

I don't think the Travis failure is related to my code.

@efiring efiring merged commit c03262a into matplotlib:master Apr 8, 2016
@efiring
Copy link
Member

efiring commented Apr 8, 2016

Thank you, @madphysicist!

@madphysicist madphysicist deleted the pctFormatter branch April 8, 2016 20:35
@madphysicist
Copy link
Contributor Author

No problem. Thanks for looking over and accepting my PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants