Skip to content

Conversation

leolchat
Copy link
Contributor

@leolchat leolchat commented Jul 19, 2020

PR Summary

Quick proposal illustrating a new definition of a concisedateformatter levels:

  • a level is computed for the offset: it is the lowest common part of the tick dates (2020-06-01 and 2020-06-02 will have month (2020-06) as offset level)
  • each tick level is computed by checking the difference from the previous (no hard coded notion of zero or relying on ticks being all spaced at the same interval or having all the same precision)

This PR is trying to not break the current user interface (format, zero, offset format strings can be changed indepently). A more comprehensive improvement to the level definition is done #17971 but it changes the user API.

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/next_api_changes/* if API changed in a backward-incompatible way

@leolchat leolchat marked this pull request as draft July 19, 2020 21:24
@leolchat
Copy link
Contributor Author

leolchat commented Jul 19, 2020

Some issues arising:
Different levels and precision between ticks mean that sometimes we need to write more than one date element (maybe month and day instead of only the day) since more can change between ticks.
This kind of conflict the notion of the user formatted strings, or they should include all level combinations which seems too much.

Maybe we could have

formats = ['%Y', '%b', '%d', '%H', '%M', '%S', '%f']
separators = [ '-', '-', ' ', 'h', ':', '.']

and print by computing the format string from that.

Then the other issue arise: it is common that things look ambiguous, hence currently the fact that level minute does print hour and minutes, that microseconds print second.microseconds, etc. Introducing an unit marker might be helpful but could be difficult to localize properly (maybe there is a python resource somewhere already for that).

Any feedbacks @jklymak ?

@leolchat leolchat force-pushed the leo_concise_levels branch from 344790c to b292933 Compare July 19, 2020 21:33
@leolchat leolchat changed the title WIP proposal WIP proposal for concise date formatter levels Jul 20, 2020
@leolchat
Copy link
Contributor Author

To play around with the outputs:

import unittest

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.dates as mdates

class MyTestCase(unittest.TestCase):
    def test_something(self):
        f = plt.figure()
        ax = plt.gca()

        dates = [np.datetime64( '2020-11-06 18:08:30'),  np.datetime64('2021-01-06 18:08:31')]
        zoom = None
        minticks = 5
        maxticks = 7
        offset = True

        # dates = [np.datetime64( '2020-11-06 18:08:30'),  np.datetime64('2021-01-06 18:08:31')]
        # zoom = np.datetime64( '2020-06-06 18:00:00.3'), np.datetime64( '2020-06-06 18:00:00.5')
        # minticks = 5
        # maxticks = 7
        # offset = True

        # dates = [np.datetime64( '2020-11-06 18:08:30'),  np.datetime64('2021-01-06 18:08:31')]
        # zoom = np.datetime64( '2020-06-06 18:08:01.3'), np.datetime64( '2020-06-06 18:08:01.5')
        # minticks = 5
        # maxticks = 7
        # offset = True

        # dates = [np.datetime64( '2020-06-06 18:08:30'),  np.datetime64('2021-01-06 18:08:31')]
        # zoom = None
        # minticks = 2
        # maxticks = 4
        # offset = True

        # dates = [np.datetime64( '2020-06-06 18:08:30'),  np.datetime64('2021-01-06 18:08:31')]
        # zoom = None
        # minticks = 10
        # maxticks = 20
        # offset = True

        ax.plot(dates, [0, 1])
        ax.set_xticks(dates)

        locator = mdates.AutoDateLocator(minticks=minticks, maxticks=maxticks)
        formatter = mdates.ConciseDateFormatter(locator, show_offset=offset)
        ax.xaxis.set_major_locator(locator)
        ax.xaxis.set_major_formatter(formatter)
        # f.autofmt_xdate()

        if zoom:
            ax.set_xlim(*zoom)
        plt.grid()
        plt.show()

if __name__ == '__main__':
    unittest.main()

@leolchat leolchat mentioned this pull request Jul 20, 2020
6 tasks
@dstansby dstansby requested a review from jklymak September 20, 2020 16:25
@jklymak jklymak removed their request for review September 21, 2020 14:39
@jklymak
Copy link
Member

jklymak commented Sep 21, 2020

@dstansby this is just a draft, and too prototypical for me to review.

@jklymak
Copy link
Member

jklymak commented Feb 16, 2021

As with #17977 I think this should/could be its own package; the differences here are largely aesthetic. Thanks for working on this!

@jklymak jklymak closed this Feb 16, 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.

3 participants