Skip to content

[Bug]: violinplot with nan values fails silently #30355

@lionel42

Description

@lionel42

Bug summary

When doing a violin plot with nan values, nothing is shown except an errror from numpy linalg

RuntimeWarning: invalid value encountered in det r = _umath_linalg.det(a, signature=signature)

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np



def set_axis_style(ax, labels):
    ax.set_xticks(np.arange(1, len(labels) + 1), labels=labels)
    ax.set_xlim(0.25, len(labels) + 0.75)
    ax.set_xlabel('Sample name')


# create test data, one with nan, one without
data = [[0,1,2,3,4,4,4], [0, 1, np.nan, 3, 4,1,1]]
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(9, 4))

ax.set_title('Default violin plot')
ax.set_ylabel('Observed values')
ax.violinplot(data)

# set style for the axes
labels = ['no nan', 'with nan']
set_axis_style(ax, labels)

plt.subplots_adjust(bottom=0.15, wspace=0.05)
plt.show()

Actual outcome

Image

I only see the first violin withouth the nan values, the one with the nan values is not plotted

Expected outcome

I would expect one of the two

  • raise an error or a warning that the data contains nan and cannot be plotted.
  • plot the violin without considering the nan values (filtering the nan values)

Additional information

So I can filter my nan data before the plot, but if you don't know you have nans you can spend quite some time figuring out what the problem with the plot is.

Should not be too difficult to implement one of the two expected outcome i suggest, but I don't know the best solution.

Operating system

windows

Matplotlib Version

3.10.3

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

Python 3.13.5

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy