Skip to content

Commit

Permalink
Merge pull request #4388 from plotly/fix-notched-box-autorange
Browse files Browse the repository at this point in the history
Fix notched box autorange
  • Loading branch information
etpinard authored Nov 26, 2019
2 parents 5ef8a61 + e663c4b commit dbf02f1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
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
}
}

0 comments on commit dbf02f1

Please sign in to comment.