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

Skip to content

Automatically create tick formatters for str and callable inputs. #16715

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 1 commit into from
May 3, 2020

Conversation

toddrjen
Copy link
Contributor

PR Summary

This allows matplotlib.axis.Axis.set_major_formatter and matplotlib.axis.Axis.set_minor_formatter to accept a str or function (really any callable) in addition to a matplotlib.ticker.Formatter. This will create and use a matplotlib.ticker.StrMethodFormatter and matplotlib.ticker.FuncFormatter, respectively. This significantly reduces the barrier to using custom formatters in simple cases.

This was previously discussed on the matplotlib-devel mailing list.

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@toddrjen toddrjen force-pushed the easyformatter branch 3 times, most recently from 3744780 to 135fb1c Compare March 10, 2020 01:34
@tacaswell tacaswell added this to the v3.3.0 milestone Mar 10, 2020
@tacaswell tacaswell requested a review from story645 March 10, 2020 02:28
@tacaswell
Copy link
Member

Thanks @toddrjen !

To try and make sure feature PRs don't fall through the cracks, we starting to assign champions who are responsible for making sure the review process goes through, @story645 has agreed to champion this PR.

@toddrjen
Copy link
Contributor Author

@tacaswell @story645 Great, thank you!

I don't know why the test coverage has dropped, I added new tests that, as far as I can tell, cover the new code paths.

Assuming this is a good approach, the big outstanding issue in my mind is whether this should be used more in the documentation. There are several examples using the old-style FormatStrFormatter that could be switched to using this approach.

@toddrjen toddrjen force-pushed the easyformatter branch 2 times, most recently from 51d0d1d to e07c5df Compare March 10, 2020 02:58
@story645
Copy link
Member

@toddrjen can you please provide some examples here cause I'm struggling a bit in finding 'em in the PR

@toddrjen
Copy link
Contributor Author

@story645 One example for the str approach used in the documentation is:

axs[5].xaxis.set_major_formatter('{x} km')

Which would replace:

from matplotlib import ticker
axs[5].xaxis.set_major_formatter(ticker.StrMethodFormatter('{x} km'))

One for the function approach used in the documentation:

axs[3].xaxis.set_major_formatter(lambda x, pos: str(x-5))

Which would replace:

from matplotlib import ticker
axs[3].xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, pos: str(x-5)))

@jklymak
Copy link
Member

jklymak commented Mar 10, 2020

In the pr it’s not clear that you need a pos argument to your function or why.

@toddrjen
Copy link
Contributor Author

@jklymak Where, exactly, do you think this should be mentioned? Can you comment on the specific lines or blocks where you think this should be changed? In most cases I am just using or following up on the existing description for the FuncFormatter.

@jklymak
Copy link
Member

jklymak commented Mar 10, 2020

@jklymak Where, exactly, do you think this should be mentioned? Can you comment on the specific lines or blocks where you think this should be changed? In most cases I am just using or following up on the existing description for the FuncFormatter.

It needs to be documented somewhere, otherwise how are people to know what a valid callable is?

@toddrjen
Copy link
Contributor Author

@jklymak Please see the updated version. I have added more explicit documentation.

@toddrjen toddrjen force-pushed the easyformatter branch 3 times, most recently from 79b548b to 43b395c Compare March 17, 2020 03:15
@toddrjen
Copy link
Contributor Author

I have added a new commit where I modified a bunch of existing examples and tutorials, and the previous documentation, to make better use of the new syntax. These changes may be more controversial so I added them to a separate commit so they would be easier to roll back.

Please take a look and see what you think.

I don't know why the tests are failing. It doesn't appear to be related to my commit but I am not sure.

@toddrjen toddrjen force-pushed the easyformatter branch 2 times, most recently from 34030d3 to e04d08a Compare March 27, 2020 16:26
@toddrjen
Copy link
Contributor Author

@story645 All tests are passing now. Is there anything else I need to do or is this ready?

@story645
Copy link
Member

story645 commented Apr 1, 2020

Sorry I still owe you feedback on this, just wanted to note that I'm +1 on this idea since learning that spotify is basically using this sort of thing as their motivating example for writing a new charting library.

Copy link
Member

@story645 story645 left a comment

Choose a reason for hiding this comment

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

a lot of especially the language changes are more suggestions than must changes, but I really want the test cases to be broken up

@toddrjen toddrjen force-pushed the easyformatter branch 2 times, most recently from bd151ee to 642928d Compare April 6, 2020 18:12
@toddrjen
Copy link
Contributor Author

toddrjen commented Apr 8, 2020

@story645 All the discussed changes should be implemented. Please take a look. I am still waiting on feedback on a few issues.

@story645
Copy link
Member

Thanks for your patience, been swamped with Passover. @jklymak and @anntzer have your concerns been addressed?

@anntzer
Copy link
Contributor

anntzer commented Apr 14, 2020

As stated above I'm basically fine with the PR, but don't like one very specific point (direct support for strings) so don't particularly want to approve it with that feature.

Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

This seems good, and I think will help 99% of folks who don't want to learn the arcana of Formatters. Do we want to replace every instance of StrMethodFormatter and FuncFormatter by this idiom? There is some advantage to pointing out the ticker module and the existence of the suite of Formatters.

@toddrjen
Copy link
Contributor Author

@jklymak

Do we want to replace every instance of StrMethodFormatter and FuncFormatter by this idiom? There is some advantage to pointing out the ticker module and the existence of the suite of Formatters.

I didn't replace every instance. examples/ticks_and_spines/tick-formatters.py still explains how to use the existing method.

@story645
Copy link
Member

Hi Todd, thanks for hanging in there! We're aiming to get this into 3.3 (rc out in a week or so).

Giving it some more thought, keeping the current formatter signature seems fine as it's consistent with the docs and what folks will find on stackoverflows. Can revisit in a later PR.

Do you feel it's ready to merge or do you have any additions to make?

@story645
Copy link
Member

Also now thinking the arguments fix should actually be on FuncFormatter since it's not clear why it needs two https://matplotlib.org/_modules/matplotlib/ticker.html#FuncFormatter

@toddrjen
Copy link
Contributor Author

toddrjen commented May 2, 2020

@story645 If all the tests pass I think it is ready to merge.

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.

6 participants