Skip to content

Commit f4b9e43

Browse files
authored
Merge pull request #6625 from lvlte/fix-contour-colorscale-6623
Fix contour plot colorscale domain
2 parents 2effef5 + aea78dd commit f4b9e43

28 files changed

+107
-15
lines changed

draftlogs/6625_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix contour plot colorscale domain (take account of [zmin, zmax] and [cmin, cmax]) [[#6625](https://github.com/plotly/plotly.js/pull/6625)], with thanks to @lvlte for the contribution!

src/components/colorbar/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ function drawColorBar(g, opts, gd) {
512512
.data(fillLevels);
513513
fills.enter().append('rect')
514514
.classed(cn.cbfill, true)
515-
.style('stroke', 'none');
515+
.attr('style', '');
516516
fills.exit().remove();
517517

518518
var zBounds = zrange

src/traces/contour/make_color_map.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ module.exports = function makeColorMap(trace) {
2929

3030
var si, i;
3131

32-
if(contours.coloring === 'heatmap') {
33-
var zmin0 = cOpts.min;
34-
var zmax0 = cOpts.max;
32+
var zmin0 = cOpts.min;
33+
var zmax0 = cOpts.max;
3534

35+
if(contours.coloring === 'heatmap') {
3636
for(i = 0; i < len; i++) {
3737
si = scl[i];
3838
domain[i] = si[0] * (zmax0 - zmin0) + zmin0;
@@ -60,11 +60,37 @@ module.exports = function makeColorMap(trace) {
6060
range.push(range[range.length - 1]);
6161
}
6262
} else {
63+
var zRangeInput = trace._input && (
64+
typeof trace._input.zmin === 'number' && typeof trace._input.zmax === 'number'
65+
);
66+
67+
// If zmin/zmax are explicitly set, consider case where user specifies a
68+
// narrower z range than that of the contours start/end.
69+
if(zRangeInput && (start <= zmin0 || end >= zmax0)) {
70+
if(start <= zmin0) start = zmin0;
71+
if(end >= zmax0) end = zmax0;
72+
nc = Math.floor((end - start) / cs) + 1;
73+
extra = 0;
74+
}
75+
6376
for(i = 0; i < len; i++) {
6477
si = scl[i];
6578
domain[i] = (si[0] * (nc + extra - 1) - (extra / 2)) * cs + start;
6679
range[i] = si[1];
6780
}
81+
82+
// Make the colorscale fit the z range except if contours are explicitly
83+
// set BUT NOT zmin/zmax.
84+
if(zRangeInput || trace.autocontour) {
85+
if(domain[0] > zmin0) {
86+
domain.unshift(zmin0);
87+
range.unshift(range[0]);
88+
}
89+
if(domain[domain.length - 1] < zmax0) {
90+
domain.push(zmax0);
91+
range.push(range[range.length - 1]);
92+
}
93+
}
6894
}
6995

7096
return Colorscale.makeColorScaleFunc(

test/image/baselines/25.png

-321 Bytes
Loading

test/image/baselines/airfoil.png

-1.24 KB
Loading
-6.29 KB
Loading
-6.29 KB
Loading
56 Bytes
Loading
Loading
-1.91 KB
Loading

0 commit comments

Comments
 (0)