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

Fix xvals for datetime axis #210

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

scottshambaugh
Copy link
Contributor

Closes #208

@cphyc do you have setup instructions for the image comparison tests? I cannot get them to fail if I change them.

@scottshambaugh
Copy link
Contributor Author

scottshambaugh commented Mar 7, 2025

Now that CI is updated I'm able to update the image test, so this PR is good to go. Would still appreciate some docs/pointers on how to get the image tests running locally!

@scottshambaugh
Copy link
Contributor Author

scottshambaugh commented Mar 7, 2025

I'll note that while I didn't want to add pandas as a dependency, this does work for pandas timestamps as well. Here was my local test script:

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

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')
ax.plot(df['t'], df['x'] + 1, label='x')
labelLines(ax.get_lines(), zorder=2.5, xvals=[df['t'].iloc[0], df['t'].iloc[-1]])  # df['t'].iloc[-1] is a pandas Timestamp
plt.show(block=True)


fig, ax = plt.subplots()
datetimes = pd.to_datetime(df['t']).values  # Creates a numpy array with dtype np.datetime64
ax.plot(datetimes, df['x'], label='x')
labelLine(ax.get_lines()[0], x=datetimes[-1])
plt.show(block=True)

@cphyc
Copy link
Owner

cphyc commented Mar 7, 2025

The easy™ solution is to run on the CI and the "correct" version will appear in the artefacts of the build which you can download and add to your PR in hindsight.

@@ -164,7 +164,7 @@ def test_dateaxis_advanced(setup_mpl):
ax.xaxis.set_major_locator(DayLocator())
ax.xaxis.set_major_formatter(DateFormatter("%Y-%m-%d"))

labelLines(ax.get_lines())
labelLines(ax.get_lines(), xvals=(dates[0], dates[-1]))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this place the first label on 2018/11/01 and the second on 2018/11/03?

Copy link
Contributor Author

@scottshambaugh scottshambaugh Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true for a list input xvals=[dates[0], dates[-1]], but the tuple input adds padding equivalent to a blank label at the extremes automatically

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

Successfully merging this pull request may close these issues.

Error with xvals argument on a time axis with Timestamp or datetime values
2 participants