Han
Han

Reputation: 725

matplotlib - violinplot ValueError with empty lists

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?

Upvotes: 6

Views: 2027

Answers (1)

fransua
fransua

Reputation: 1608

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..

Upvotes: 5

Related Questions