Skip to content
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

Error with xvals argument on a time axis with Timestamp or datetime values #208

Open
scottshambaugh opened this issue Mar 6, 2025 · 2 comments · May be fixed by #210
Open

Error with xvals argument on a time axis with Timestamp or datetime values #208

scottshambaugh opened this issue Mar 6, 2025 · 2 comments · May be fixed by #210
Labels

Comments

@scottshambaugh
Copy link
Contributor

scottshambaugh commented Mar 6, 2025

If I try to use the xvals argument by supplying time information on a time axis, I get errors. This happens with both a pandas Timestamps and datetime64 objects. Adding labels in both cases without specifying the xvals is working just fine.

Example with pandas:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from labellines import labelLines

x = np.linspace(0, 10, 100)
t = pd.date_range(start='2020-01-01', periods=100)  # Creates a DatetimeIndex
df = pd.DataFrame({'x': x, 't': t})

fig, ax = plt.subplots()

ax.plot(df['t'], df['x'], label='x')
labelLines(ax.get_lines(), zorder=2.5, xvals=df['t'].iloc[0])  # df['t'].iloc[0] is a pandas Timestamp

plt.show()

Results in this error:

Exception has occurred: TypeError
'<=' not supported between instances of 'numpy.float64' and 'Timestamp'
  File "/home/scott/coding/_test.py", line 13, in <module>
    labelLines(ax.get_lines(), zorder=2.5, xvals=df['t'][0])
TypeError: '<=' not supported between instances of 'numpy.float64' and 'Timestamp'

And then this substitution to show datetimes:

datetimes = pd.to_datetime(df['t']).values  # Creates a numpy array with dtype np.datetime64
ax.plot(datetimes, df['x'], label='x')
labelLines(ax.get_lines(), zorder=2.5, xvals=datetimes[0])

Results in this error:

ufunc 'less_equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.DateTime64DType'>) -> None
  File "/home/scott/coding/_test.py", line 15, in <module>
    labelLines(ax.get_lines(), zorder=2.5, xvals=datetimes[0])
numpy._core._exceptions._UFuncNoLoopError: ufunc 'less_equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.DateTime64DType'>) -> None
@scottshambaugh scottshambaugh changed the title Error with xvals argument when plotting on a pandas DatetimeIndex or a axis Error with xvals argument on a time axis with Timestamp or datetime values Mar 6, 2025
@cphyc cphyc added the bug label Mar 7, 2025
@cphyc
Copy link
Owner

cphyc commented Mar 7, 2025

Thanks for the report. This is indeed a bug. Did you manage to fix it manually? I never work with timestamps myself.

@scottshambaugh scottshambaugh linked a pull request Mar 7, 2025 that will close this issue
@scottshambaugh
Copy link
Contributor Author

Sure did, and PR opened for a fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants