Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d85a9a4

Browse files
committed
Merge pull request #4816 from brunobeltran/master
FIX: violinplot crashed if input variance was zero
2 parents 8242f57 + b30ff74 commit d85a9a4

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7165,6 +7165,9 @@ def violinplot(self, dataset, positions=None, vert=True, widths=0.5,
71657165
"""
71667166

71677167
def _kde_method(X, coords):
7168+
# fallback gracefully if the vector contains only one value
7169+
if np.all(X[0] == X):
7170+
return (X[0] == coords).astype(float)
71687171
kde = mlab.GaussianKDE(X, bw_method)
71697172
return kde.evaluate(coords)
71707173

lib/matplotlib/tests/test_axes.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3895,6 +3895,11 @@ def test_shared_scale():
38953895
assert_equal(ax.get_yscale(), 'linear')
38963896
assert_equal(ax.get_xscale(), 'linear')
38973897

3898+
@cleanup
3899+
def test_violin_point_mass():
3900+
"""Violin plot should handle point mass pdf gracefully."""
3901+
plt.violinplot(np.array([0, 0]))
3902+
38983903
if __name__ == '__main__':
38993904
import nose
39003905
import sys

0 commit comments

Comments
 (0)