Skip to content

DOC: Add Date Tick Locators and Formatters example - #21874

Merged
jklymak merged 1 commit into
matplotlib:mainfrom
StefRe:date_fmt-loc_reference
Dec 7, 2021
Merged

jklymak merged 1 commit into
matplotlib:mainfrom
StefRe:date_fmt-loc_reference

Conversation

@StefRe

@StefRe StefRe commented Dec 6, 2021

Copy link
Copy Markdown
Contributor

PR Summary

Add Date Tick Locators and Formatters example. Closes #17283.

I noticed that

class rrulewrapper:
def __init__(self, freq, tzinfo=None, **kwargs):
has no docstring and hence can't be referred to in the documentation (see admonition:: References; the same pertains to
* `RRuleLocator`: Locate using a `matplotlib.dates.rrulewrapper`.
).

grafik


grafik

PR Checklist

Tests and Styling

  • [N/A] Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (install flake8-docstrings and run flake8 --docstring-convention=all).

Documentation

  • [N/A] New features are documented, with examples if plot related.
  • [N/A] New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • [N/A] API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).

@jklymak jklymak added this to the v3.6.0 milestone Dec 6, 2021

@dstansby dstansby left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks great! Using eval() seems a bit odd to me - is there a reason that locators and formatters can't contain the actual locator/formatter instances instead of their eval strings?

@StefRe

StefRe commented Dec 7, 2021 •

Copy link
Copy Markdown
Contributor Author

@dstansby the eval is just to prevent redundant definition of the string 'AutoDateLocator(maxticks=8)' and the locator itself, i.e. it's meant as a shortcut of writing e.g.

locators = [
    ('AutoDateLocator(maxticks=8)', AutoDateLocator(maxticks=8), '2003-02-01', '%Y-%m'),
    ...

Maybe using eval is not really best practice but it reduces the possibility of mistakes/inconsistencies between the code and the text in this case.

@jklymak

jklymak commented Dec 7, 2021

Copy link
Copy Markdown
Member

I guess I but the argument that the eval is useful here...

@story645

story645 commented Dec 7, 2021 •

Copy link
Copy Markdown
Member

Maybe using eval is not really best practice but it reduces the possibility of mistakes/inconsistencies between the code and the text in this case.

Would a repr on the objects (assuming it exists) print anything different from the eval?

@StefRe

StefRe commented Dec 7, 2021

Copy link
Copy Markdown
Contributor Author

@story645 repr(AutoDateLocator(maxticks=8)) gives '<matplotlib.dates.AutoDateLocator object at 0x000001CE4F687CA0>'.

Defining nice reprs wouldn't be enough, however, as we also need the arguments of the function calls.

@story645

story645 commented Dec 7, 2021

Copy link
Copy Markdown
Member

Defining nice reprs wouldn't be enough, however, as we also need the arguments of the function calls.

Why couldn't those be in the repr?

@StefRe

StefRe commented Dec 7, 2021

Copy link
Copy Markdown
Contributor Author

@story645
I only have very basic knowledge of python in this respect, maybe I'm completely wrong:

When I try AutoDateLocator.__repr__ = lambda self: f'AutodateLocator(maxticks={self.maxticks})' then repr(AutoDateLocator(maxticks=8)) returns 'AutodateLocator(maxticks={0: 8, 1: 8, 3: 8, 4: 8, 5: 8, 6: 8, 7: 8})', i.e. the class variable maxticks. I don't know how to get the value passed in during the function call. But even if this is possible, I find defining all the repr rather verbose and a bit hacky as compared to a simple eval.

@dstansby

dstansby commented Dec 7, 2021

Copy link
Copy Markdown
Member

In an ideal world we would define sensible __repr__s on each of the locators/formatters so they could be used - I think that's a stretch goal, and this PR is worth considering regardless of that.

@jklymak
jklymak merged commit fb5b91e into matplotlib:main Dec 7, 2021
@jklymak

jklymak commented Dec 7, 2021

Copy link
Copy Markdown
Member

Thanks @StefRe for your very helpful contributions!

@StefRe
StefRe deleted the date_fmt-loc_reference branch December 8, 2021 07:17
@StefRe

StefRe commented Dec 8, 2021

Copy link
Copy Markdown
Contributor Author

@jklymak Thanks!
What about the rrulewrapper issue I mentioned at the very beginning? Open a new issue for this? Should I (for the time being) remove the reference to rrulewrapper as there's no link:
grafik

@jklymak

jklymak commented Dec 8, 2021

Copy link
Copy Markdown
Member

Sure, open an issue. Seems the solution is to add a docstring to rrulewrapper if it is public API...

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.

Create Date Formatter/Locator Reference

4 participants