5

I am getting the following ValueError when I try to plot a "violin plot" with matplotlib.

ValueError: zero-size array to reduction operation minimum which has no identity

axes[0].violinplot([[1,2,3],[],[2,3,4]])

I hope two violin plots to be plotted on the left and right sides, and something to be plotted in the middle to represent the invalid items.

What should I do to overcome this?

Han
  • 625
  • 2
  • 7
  • 25

1 Answers1

4

you could check if the list is empty, and if it's the case replace it by a list of NaNs:

from matplotlib import pyplot as plt

vals = [[1, 2, 3], [], [2, 3, 4]]
nans = [float('nan'), float('nan')] # requires at least 2 nans

plt.violinplot([val or nans for val in vals])
plt.show()

It's not a really elegant option, but it works..

fransua
  • 1,559
  • 13
  • 30
  • Somehow, I was getting ' ValueError: The truth value of an array with more than one element is ambiguous. ' . Using `[ val if val.any() else nans for val in vals ]` made my day. – acapet Nov 11 '21 at 20:12