Skip to content
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

Fix notched box autorange #4388

Merged
merged 2 commits into from
Nov 26, 2019
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
7 changes: 6 additions & 1 deletion src/traces/box/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ module.exports = {
role: 'style',
editType: 'calc',
description: [
'Determines whether or not notches should be drawn.'
'Determines whether or not notches are drawn.',
'Notches displays a confidence interval around the median.',
'We compute the confidence interval as median +/- 1.57 / IQR * sqrt(N),',
'where IQR is the interquartile range and N is the sample size.',
'If two boxes\' notches do not overlap there is 95% confidence their medians differ.',
'See https://sites.google.com/site/davidsstatistics/home/notched-box-plots for more info.'
].join(' ')
},
notchwidth: {
Expand Down
12 changes: 10 additions & 2 deletions src/traces/box/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ module.exports = function calc(gd, trace) {
Lib.identity :
function(pt) { return (pt.v < cdi.lf || pt.v > cdi.uf); };

var minLowerNotch = Infinity;
var maxUpperNotch = -Infinity;

// build calcdata trace items, one item per distinct position
for(i = 0; i < pLen; i++) {
if(ptsPerBin[i].length > 0) {
Expand Down Expand Up @@ -123,6 +126,8 @@ module.exports = function calc(gd, trace) {
var mci = 1.57 * iqr / Math.sqrt(bvLen);
cdi.ln = cdi.med - mci;
cdi.un = cdi.med + mci;
minLowerNotch = Math.min(minLowerNotch, cdi.ln);
maxUpperNotch = Math.max(maxUpperNotch, cdi.un);

cdi.pts2 = pts.filter(ptFilterFn);

Expand All @@ -131,8 +136,11 @@ module.exports = function calc(gd, trace) {
}

calcSelection(cd, trace);
var extremes = Axes.findExtremes(valAxis, val, {padded: true});
trace._extremes[valAxis._id] = extremes;

trace._extremes[valAxis._id] = Axes.findExtremes(valAxis,
trace.notched ? val.concat([minLowerNotch, maxUpperNotch]) : val,
{padded: true}
);

if(cd.length > 0) {
cd[0].t = {
Expand Down
Binary file added test/image/baselines/box_notched-inverted-end.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions test/image/mocks/box_notched-inverted-end.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"data": [
{
"x": [113.35, 29.54, 21.7, 113.35, 29.54, 21.7],
"name": "[113.35,29.54,21.7,113.35,29.54,21.7]",
"type": "box",
"notched": true
},
{
"x": [39.4, 88.63, 39.4, 88.63],
"name": "[39.4,88.63,39.4,88.63]",
"type": "box",
"notched": true
}
],
"layout": {
"title": {
"text": "Samples where the confidence interval<br>median ± 1.57 / IQR * sqrt(N)<br>go beyond the fences",
"x": 0.02
},
"xaxis": {
"showline": true,
"mirror": true,
"zeroline": false
},
"yaxis": {
"showline": true,
"mirror": true,
"zeroline": false
},
"showlegend": false,
"margin": {"l": 220, "b": 20, "r": 20},
"width": 600,
"height": 300
}
}