Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sdarougheh opened this issue Jul 24, 2024 · 6 comments · Fixed by #28720
Closed

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

sdarougheh opened this issue Jul 24, 2024 · 6 comments · Fixed by #28720
Labels
Documentation: API files in lib/ and doc/api status: needs clarification Issues that need more information to resolve.

Comments

@sdarougheh
Copy link

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

@QuLogic
Copy link
Member

QuLogic commented Jul 24, 2024

xmin and xmax are in Axes units, not data units; using a datetime there would not work. Are you sure you didn't mean ax.axhline(dt)?

The same error would occur with ax.axvline(0, dt), since ymin/ymax have the same usage.

@sdarougheh
Copy link
Author

sdarougheh commented Jul 25, 2024

I know that it doesn't work currently -- that is why it's a feature request. It appears very odd that I can easily specify my vertical line on a data coordinate, but if I want to specify the start and end points of a horizontal line, I can no longer use data coordinates.

@QuLogic
Copy link
Member

QuLogic commented Jul 25, 2024

There is no difference between axvline and axhline; you are calling them differently and passing your datetime to a different argument that does a different thing.

axhline and axvline are meant to be in mixed coordinate spaces, one data space and one Axes space. If you are trying to use data coordinates for both, then you should use plot.

@Devanshi-Bavaria
Copy link

To use axhline effectively with dates on the x-axis, you'll need to simulate a horizontal line across a range of dates by combining axhline with ax.set_xlim( ). Since axhline doesn't directly support date ranges for xmin and xmax, you use axhline to draw the line at a specific y-value and then adjust the x-axis limits to ensure that the horizontal line spans the desired range of dates.

Here’s how to do it:

  • Draw the Horizontal Line: Use axhline to add the line at a specific y-value.
  • Adjust X-Axis Limits: Ensure that the x-axis limits include the range of dates you want to span.

@QuLogic QuLogic added the status: needs clarification Issues that need more information to resolve. label Aug 14, 2024
@timhoffm
Copy link
Member

timhoffm commented Aug 14, 2024

As mentioned above, axhline x limits are in Axes units (span the full or a fraction of the axis). It's not intended for data units. Use plot([xmin, xmax], [y, y]) or hlines(y, xmin, xmax) if you want x in data units.

Action: The documentation should be improved axhline reads "Add a horizontal line across the Axes", which is technically correct, but it's not obvious that it's not for data coordinates. We should make this more clear.

@timhoffm timhoffm added Documentation: API files in lib/ and doc/api and removed New feature labels Aug 14, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Aug 14, 2024
timhoffm added a commit to timhoffm/matplotlib that referenced this issue Aug 14, 2024
@Devanshi-Bavaria
Copy link

As mentioned above, axhline x limits are in Axes units (span the full or a fraction of the axis). It's not intended for data units. Use plot([xmin, xmax], [y, y]) or hlines(y, xmin, xmax) if you want x in data units.

Action: The documentation should be improved axhline reads "Add a horizontal line across the Axes", which is technically correct, but it's not obvious that it's not for data coordinates. We should make this more clear.

You're correct that axhline is designed to work with axes units rather than data units, which can be a bit confusing when you want to place a horizontal line based on specific data points on the x-axis.

Summary

  • axhline: Best for when you want a horizontal line across a fraction of the axis, not specific data values.

  • plot([xmin, xmax], [y, y]) and hlines(y, xmin, xmax): These should be used when you want your line's start and end points to correspond to specific x-axis data values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: API files in lib/ and doc/api status: needs clarification Issues that need more information to resolve.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants