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

ValueError with labellines when plotting rolling mean with NaNs #217

Closed
Vasco-Da-Gama-png opened this issue Mar 14, 2025 · 2 comments · Fixed by #220
Closed

ValueError with labellines when plotting rolling mean with NaNs #217

Vasco-Da-Gama-png opened this issue Mar 14, 2025 · 2 comments · Fixed by #220

Comments

@Vasco-Da-Gama-png
Copy link

Bug Report: ValueError with labellines when plotting rolling mean with NaNs

Description

When using the labellines package to label lines in a plot that includes a rolling mean with NaN values, a ValueError is raised with the message "x label location is outside data range". This issue occurs even though the commit 0a517f7 was supposed to handle NaN values.

Minimal Example

The following minimal example demonstrates the issue. It creates a DataFrame with random values and some NaN values, calculates the rolling mean, and then attempts to label the lines using labellines.

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

# Create a DataFrame with random values and NaNs
np.random.seed(0)
time = pd.date_range(start="2025-01-01", periods=100, freq="T")
data = np.random.randn(100).cumsum()
data[20:30] = np.nan  # Add some NaN values

df = pd.DataFrame({"Time": time, "Value": data})

# Calculate the rolling mean
roll_mean = df["Value"].rolling(window=10).mean()

# Plot the data and the rolling mean
fig, ax = plt.subplots()
ax.plot(df["Time"], df["Value"], label="Original Data", alpha=0.5)
ax.plot(df["Time"], roll_mean, label="Rolling Mean", linewidth=2)

# Use labellines to label the lines
try:
    labelLines(ax.get_lines(), align=False)
except ValueError as e:
    print(f"Error: {e}")

# Show the plot
plt.show()

Expected Behavior

The labellines function should label the lines without raising a ValueError, even when the data contains NaN values.

Actual Behavior

A ValueError is raised with the message "x label location is outside data range".

Environment

matplotlib version: [3.10.1]
labellines version: [Github Main branch]
Python version: [3.11.4]
Operating System: [Debian buster]

Additional Information

The issue seems to be related to the handling of NaN values in the data. The commit 0a517f7 was supposed to address this, but the problem persists when plotting rolling means.

@scottshambaugh
Copy link
Contributor

Thanks for the script to reproduce the error! It was a simple fix, just pushed up a PR to resolve it.

@Vasco-Da-Gama-png
Copy link
Author

Thanks for fixing this as well. I already implemented your fix in my local code and it works great!

@cphyc cphyc closed this as completed in #220 Apr 3, 2025
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 a pull request may close this issue.

2 participants