Skip to content

[ENH]: matplotlib.pyplot.axvline does not support datetime for xmin/xmax #28612

@sdarougheh

Description

@sdarougheh

Problem

I am trying to create a figure with dates on the x-axis.

import matplotlib.pyplot as plt
import pandas as pd
fig, ax = plt.subplots()
dt = pd.to_datetime('1980-01-01 00:00:00')

Matplotlib already supports entering datetime as x-coordinate for vertical lines:

ax.axvline(dt)

But it does not allow me to use datetimes as xmin and xmax for horizontal lines:

ax.axhline(0, dt)
  File "/home/miniconda3/envs/stata/lib/python3.12/site-packages/matplotlib/axes/_axes.py", line 773, in axhline
    self._check_no_units([xmin, xmax], ['xmin', 'xmax'])
  File "/home/miniconda3/envs/stata/lib/python3.12/site-packages/matplotlib/axes/_axes.py", line 863, in _check_no_units
    raise ValueError(f"{name} must be a single scalar value, "
ValueError: xmin must be a single scalar value, but got 1980-01-01 00:00:00

Proposed solution

No response

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