Skip to content

[Bug]: linelengths parameter of Axes.eventplot does not accept delta units #27479

Open
@QuLogic

Description

@QuLogic

Bug summary

Based on #27466, the linelengths parameter of Axes.eventplot does not accept delta units, such as datetime.timedelta. This should work, as linelengths is something similar to width in bar.

Code for reproduction

fig, ax = plt.subplots(layout="constrained")

x_dates1 = np.array(
    [datetime.datetime(2020, 6, 30), datetime.datetime(2020, 7, 22),
     datetime.datetime(2020, 8, 3), datetime.datetime(2020, 9, 14),],
    dtype=np.datetime64)

np.random.seed(19680801)

start_date = datetime.datetime(2020, 7, 1)
end_date = datetime.datetime(2020, 10, 15)
date_range = end_date - start_date

dates1 = start_date + np.random.rand(30) * date_range
dates2 = start_date + np.random.rand(10) * date_range
dates3 = start_date + np.random.rand(50) * date_range

colors = ['C1', 'C2', 'C3']
lineoffsets = np.array([
    datetime.datetime(2020, 7, 1),
    datetime.datetime(2020, 7, 15),
    datetime.datetime(2020, 8, 1)
], dtype=np.datetime64)
linelengths = [datetime.timedelta(days=5), datetime.timedelta(days=2),
               datetime.timedelta(days=3)]

ax.eventplot([dates1, dates2, dates3], colors=colors,
             lineoffsets=lineoffsets, linelengths=linelengths)

Actual outcome

Traceback (most recent call last):
  File "/home/elliott/code/matplotlib/lib/matplotlib/axis.py", line 1766, in convert_units
    ret = self.converter.convert(x, self.units, self)
  File "/home/elliott/code/matplotlib/lib/matplotlib/dates.py", line 1829, in convert
    return self._get_converter().convert(*args, **kwargs)
  File "/home/elliott/code/matplotlib/lib/matplotlib/dates.py", line 1757, in convert
    return date2num(value)
  File "/home/elliott/code/matplotlib/lib/matplotlib/dates.py", line 449, in date2num
    d = d.astype('datetime64[us]')
ValueError: Could not convert object to NumPy datetime

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/elliott/code/matplotlib/foo.py", line 31, in <module>
    ax.eventplot([dates1, dates2, dates3], colors=colors,
  File "/home/elliott/code/matplotlib/lib/matplotlib/__init__.py", line 1472, in inner
    return func(
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_axes.py", line 1353, in eventplot
    lineoffsets, linelengths = self._process_unit_info(
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_base.py", line 2566, in _process_unit_info
    return [axis_map[axis_name].convert_units(data)
  File "/home/elliott/code/matplotlib/lib/matplotlib/axes/_base.py", line 2566, in <listcomp>
    return [axis_map[axis_name].convert_units(data)
  File "/home/elliott/code/matplotlib/lib/matplotlib/axis.py", line 1768, in convert_units
    raise munits.ConversionError('Failed to convert value(s) to axis '
matplotlib.units.ConversionError: Failed to convert value(s) to axis units: [datetime.timedelta(days=5), datetime.timedelta(days=2), datetime.timedelta(days=3)]

Expected outcome

The plot should work, with some kind of date-based scale on the vertical axis.

Additional information

No response

Operating system

No response

Matplotlib Version

a2a2809

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

git checkout

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