Skip to content

Conversation

@codingabhiroop
Copy link
Contributor

@codingabhiroop codingabhiroop commented Jan 4, 2026

PR summary

violinplot currently fails silently when a dataset contains NaN values, aside
from a low-level NumPy warning, which can be confusing for users.

This PR filters NaN values in cbook.violin_stats, emits a RuntimeWarning,
and allows violins with valid data to be plotted. Datasets consisting entirely
of NaNs are skipped.

This behavior is consistent with other plotting functions such as hist.

CI Note: Failing tests (WebAgg, GTK/Tk/Qt backends, Windows_py312) are unrelated to this PR.
This PR only touches core plotting logic and test_axes.py. Failures are due to headless/ARM/Windows CI environments.

PR checklist

  • closes [Bug]: violinplot with nan values fails silently #30355
  • new and changed code is tested
  • [N/A] Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • [N/A] Documentation complies with general and docstring guidelines

@timhoffm
Copy link
Member

timhoffm commented Jan 6, 2026

Reconsidering: Do we need to warn at all? This is a bit of a philosophic question: Is NaN considered just a placeholder for "no value"? In that case it is reasonably to silently drop NaNs. Or is NaN marking "undefined value" in which case a warning would be reasonable.

Since plt.plot() and plt.hist() do not warn on NaNs I assume we tend to go with the first interpretation, even though we have never spelled that out explicitly. Do we have precedence for warning on NaNs?

@scottshambaugh
Copy link
Contributor

My preference would also be to not warn and instead skip NaN values, like we do for hist. Sorry @codingabhiroop, I think this should get discussed more in the original issue before we commit to a solution

@codingabhiroop
Copy link
Contributor Author

Thanks for the clarification — that makes sense.

Happy to hold off on further changes until there’s agreement in the original issue, and I’m also happy to update this PR like removing the warning entirely once a direction is decided.

@timhoffm
Copy link
Member

timhoffm commented Jan 9, 2026

I'll decide here: silently dropping NaNs is the way to go.

raise ValueError("List of violinplot statistics and quantiles values"
" must have the same length")

has_nan = any(np.isnan(np.asarray(x)).any() for x in X)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
has_nan = any(np.isnan(np.asarray(x)).any() for x in X)
has_nan = any(np.isnan(x).any() for x in X)

I suspect asarray() does not have any benefit here as isnan() accepts array-like.



def test_violinplot_nan_values_warn_and_plot():
data = [[0, 1, 2, 3], [1, np.nan, 2, 3]]
Copy link
Member

Choose a reason for hiding this comment

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

Please also test the case of a single 1D input data = [1, np.nan, 2, 3]


# If all values are NaN, skip this violin
if x.size == 0:
continue
Copy link
Member

Choose a reason for hiding this comment

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

This would swallow the vpstats.append() at the end, which means you loose all-nan data columns. Instead, we must create a stats entry with nan for all values.

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.

[Bug]: violinplot with nan values fails silently

4 participants