-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Open
Description
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

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
Labels
No labels