-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
Community supportUsers in need of help.Users in need of help.
Description
Bug summary
When setting xmax or xmin during an axhline call on a datetime axis, datetime limits are not accepted. This also happens with axvline.
Code for reproduction
import matplotlib.pyplot as plt
from datetime import datetime
fig, ax = plt.subplots()
start_date = datetime(2023, 5, 1)
end_date = datetime(2023, 5, 7)
ax.plot([start_date, end_date], [-1, 1])
ax.axhline(0, xmin=start_date, xmax=end_date)Actual outcome
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In [16], line 9
7 end_date = datetime(2023, 5, 7)
8 ax.plot([start_date, end_date], [-1, 1])
----> 9 ax.axhline(0, xmin=start_date, xmax=end_date)
File ~/anaconda3/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:761, in Axes.axhline(self, y, xmin, xmax, **kwargs)
710 @_docstring.dedent_interpd
711 def axhline(self, y=0, xmin=0, xmax=1, **kwargs):
712 """
713 Add a horizontal line across the Axes.
714
(...)
759 >>> axhline(y=.5, xmin=0.25, xmax=0.75)
760 """
--> 761 self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
762 if "transform" in kwargs:
763 raise ValueError("'transform' is not allowed as a keyword "
764 "argument; axhline generates its own transform.")
File ~/anaconda3/envs/py310/lib/python3.10/site-packages/matplotlib/axes/_axes.py:851, in Axes._check_no_units(vals, names)
849 for val, name in zip(vals, names):
850 if not munits._is_natively_supported(val):
--> 851 raise ValueError(f"{name} must be a single scalar value, "
852 f"but got {val}")
ValueError: xmin must be a single scalar value, but got 2023-05-01 00:00:00Expected outcome
The below, but with the horizontal line truncating at the limits.

Additional information
No response
Operating system
No response
Matplotlib Version
3.7.1
Matplotlib Backend
No response
Python version
3.10
Jupyter version
No response
Installation
pip
Metadata
Metadata
Assignees
Labels
Community supportUsers in need of help.Users in need of help.
