Skip to content

Fix bandwidth for single-value violins #2775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/traces/violin/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function silvermanRule(len, ssd, iqr) {
function calcBandwidth(trace, cdi, vals) {
var span = cdi.max - cdi.min;

// plot single-value violin with bandwidth of 1
if(!span) return 1;

// Limit how small the bandwidth can be.
//
// Silverman's rule of thumb can be "very" small
Expand Down
19 changes: 19 additions & 0 deletions test/jasmine/tests/violin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,25 @@ describe('Test violin calc:', function() {
expect(fullLayout._violinScaleGroupStats.one.maxWidth).toBeCloseTo(0.055);
expect(fullLayout._violinScaleGroupStats.one.maxCount).toBe(8);
});

it('handle multi-box / single-value case', function() {
_calc({
x: [1, 2, 3, 4, 5, 6],
y: [1, 2, 3, 4, 5, 6]
});

expect(cd.length).toBe(6, '# of violins');
expect(cd.every(function(d) { return d.bandwidth; })).toBe(true, 'bandwidth');
});

it('handle multi-value / single-but-unique-value case', function() {
_calc({
y: [1, 1, 1, 1, 1]
});

expect(cd.length).toBe(1, '# of violins');
expect(cd[0].bandwidth).toBe(1, 'bandwidth');
});
});

describe('Test violin hover:', function() {
Expand Down