Visitar URL original
`ax.xaxes.axis_date()` fails with `tz` `TypeError` error when units are not recognized/not correctly parsed · Issue #25419 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20250616002249/https://github.com/matplotlib/matplotlib/issues/25419
Skip to content

ax.xaxes.axis_date() fails with tz TypeError error when units are not recognized/not correctly parsed #25419

Closed
@valeriupredoi

Description

@valeriupredoi

Hello guys, here is a minimal test case you can run see the behaviour: this is for Matplotlib=3.7.1, previous Matplotlbs <3.6 worked fine wrt this:

import matplotlib.pyplot as plt


def test_axes_goes():
    ax = plt.gca()
    ax.xaxis.set_units(None)
    ax.xaxis.axis_date(tz='UTC')


def test_axes_fails():
    ax = plt.gca()
    ax.xaxis.set_units(('proleptic_gregorian', 'days since 2000-01-01'))
    ax.xaxis.axis_date(tz='UTC')


if __name__ == '__main__':
    test_axes_goes()
    test_axes_fails()

this fails with:

Traceback (most recent call last):
  File "/home/b/b382109/test_matplotlib.py", line 18, in <module>
    test_axes_fails()
  File "/home/b/b382109/test_matplotlib.py", line 12, in test_axes_fails
    ax.xaxis.set_units(('proleptic_gregorian', 'days since 2000-01-01'))
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/axis.py", line 1763, in set_units
    axis._update_axisinfo()
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/axis.py", line 1692, in _update_axisinfo
    info = self.converter.axisinfo(self.units, self)
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/dates.py", line 1933, in axisinfo
    return self._get_converter().axisinfo(*args, **kwargs)
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/dates.py", line 1850, in axisinfo
    majloc = AutoDateLocator(tz=tz,
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/dates.py", line 1339, in __init__
    super().__init__(tz=tz)
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/dates.py", line 1136, in __init__
    tzx = _get_tzinfo(tz)
  File "/home/b/b382109/miniconda3/envs/release280rc1/lib/python3.10/site-packages/matplotlib/dates.py", line 234, in _get_tzinfo
    raise TypeError("tz must be string or tzinfo subclass.")
TypeError: tz must be string or tzinfo subclass.

There are two things two unpack here:

  • the code should probably not fail with a TimeZone-related error, least a TypeError since I am clearly defining my tz arg
  • if the units are not recognized by Matplotlib, as this is the case, since those are cf_units from an iris cube, there should be a warning or raiser at ax.xaxis.set_units() step

Cheers muchly for all your good work! 🍺

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions