Skip to content

mdates.ConciseDateFormatter() doesn't work with zero_formats parameter #15178

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

Closed
fifzteen opened this issue Sep 4, 2019 · 5 comments · Fixed by #15181
Closed

mdates.ConciseDateFormatter() doesn't work with zero_formats parameter #15178

fifzteen opened this issue Sep 4, 2019 · 5 comments · Fixed by #15181
Assignees
Milestone

Comments

@fifzteen
Copy link

fifzteen commented Sep 4, 2019

Bug report

Bug summary

mdates.ConciseDateFormatter() raises TypeError when giving zero_formats parameter.

Code for reproduction

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

ny = 2
xmin, xmax = mdates.datestr2num(['2019-09-04 00:00:00', '2019-09-05 00:00:00'])
data = np.random.random((ny, int(xmax-xmin)+1))

fig, ax = plt.subplots()
ax.imshow(data, extent=[xmin, xmax, 0, ny], aspect='auto')
xlocator = mdates.AutoDateLocator()
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(xlocator, zero_formats=['', '%Y', '%m', '%m-%d', '%H:%M', '%H:%M']))
plt.show()

Actual outcome

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\foobar\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\dates.py", line 842, in __init__
    if len(formats) != 6:
TypeError: object of type 'NoneType' has no len()

Expected

dates.py around line 842 below.

841 if zero_formats:
842    if len(formats) != 6:
843        raise ValueError('zero_formats argument must be a list of '
844                                  '6 format strings (or None)')
845    self.zero_formats = zero_formats

now, formats is None (dafeult value). so, line 842 shuold be

842    if len(zero_formats) != 6:

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.1.1
  • Python version: 3.7.4
@jklymak
Copy link
Member

jklymak commented Sep 4, 2019

Hah I just copied that code over. I had no idea what zero format is. If you think it’s useful to make work with the formatter we should fix it, or if not, remove it. Feel free to make a PR if you think you understand the issue well enough, otherwise I’ll try to fix in
A few weeks. Thanks for the report

@fifzteen
Copy link
Author

fifzteen commented Sep 4, 2019

Thanks for your reply.

I simplify abobe example code to look the problem clearly.
What I want to get is a plot like below.
Figure_1

"zero_formats" is useful to draw date and time only the time is 0:00. like this code.

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

ny = 2
xmin, xmax = mdates.datestr2num(['2019-09-04 00:00:00', '2019-09-05 00:00:00'])
data = np.random.random((ny, int(xmax-xmin)+1))

fig, ax = plt.subplots()
ax.imshow(data, extent=[xmin, xmax, 0, ny], aspect='auto')
xlocator = mdates.AutoDateLocator()
ax.xaxis.set_major_locator(xlocator)
ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(xlocator,
                                                         formats=['%Y', '%b', '%d', '%H:%M', '%H:%M', '%S.%f'],
                                                         zero_formats=['', '%Y', '%m', '%m-%d\n%H:%M', '%H:%M', '%H:%M'],
                                                         show_offset=False))
ax.tick_params(axis='x', labelrotation=90.)
plt.show()

(We can prevent this problem by setting formats to default value.)

I don't know how many effects to other codes are exists. so I think it's better that you fix this bug.

@jklymak
Copy link
Member

jklymak commented Sep 4, 2019

Yeah but that’s what concisedateformatter is supposed to do automatically so I’m not sure the kwarg makes sense at all?

@jklymak
Copy link
Member

jklymak commented Sep 4, 2019

OK, duh, I added zero_formats. Sorry, should have looked at the code. This is a definite bug. I'll fix ASAP.

@jklymak
Copy link
Member

jklymak commented Sep 4, 2019

Thanks for the report, and sorry about my confusion above. This should be fixed in 3.1.2, in the meantime, your workaround is correct.

@QuLogic QuLogic added this to the v3.1.2 milestone Sep 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants