matplotlib / matplotlib/matplotlib
[Bug]: linelengths parameter of Axes.eventplot does not accept delta units
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 23.2k
- Forks
- 8.5k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 66
Description
### 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
```python
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
```pytb
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
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
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
a2a28093ba836366e7ff0c6f1edb4fd98b47a04d
### Matplotlib Backend
_No response_
### Python version
_No response_
### Jupyter version
_No response_
### Installation
git checkout
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/matplotlib/axes/_axes.py at Axes.eventplot and trace the unit handling shown through lib/matplotlib/axes/_base.py, lib/matplotlib/axis.py, and lib/matplotlib/dates.py. Run the provided reproduction to confirm the failure. Done means datetime.timedelta values are accepted for linelengths and the event plot renders with a date-based vertical scale.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data-visualization
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100