Skip to content

Commit

Permalink
Merge pull request #5985 from andrew-matteson/log-contour
Browse files Browse the repository at this point in the history
Interpolate contours in linear space, not data space
  • Loading branch information
archmoj committed Oct 19, 2021
2 parents 354a7a1 + 1dea4a7 commit aef563b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions draftlogs/5985_change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Improve drawing the contour lines in non-linear space e.g. on log axes [[#5985](https://github.com/plotly/plotly.js/pull/5985)], with thanks to @andrew-matteson for the contribution!
13 changes: 11 additions & 2 deletions src/traces/contour/find_all_paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,25 @@ function getInterpPx(pi, loc, step) {
var xa = pi.xaxis;
var ya = pi.yaxis;

// Interpolate in linear space, then convert to pixel
if(step[1]) {
var dx = (pi.level - zxy) / (pi.z[locy][locx + 1] - zxy);
// Interpolate, but protect against NaN linear values for log axis (dx will equal 1 or 0)
var dxl =
(dx !== 1 ? (1 - dx) * xa.c2l(pi.x[locx]) : 0) +
(dx !== 0 ? dx * xa.c2l(pi.x[locx + 1]) : 0);

return [xa.c2p((1 - dx) * pi.x[locx] + dx * pi.x[locx + 1], true),
return [xa.c2p(xa.l2c(dxl), true),
ya.c2p(pi.y[locy], true),
locx + dx, locy];
} else {
var dy = (pi.level - zxy) / (pi.z[locy + 1][locx] - zxy);
var dyl =
(dy !== 1 ? (1 - dy) * ya.c2l(pi.y[locy]) : 0) +
(dy !== 0 ? dy * ya.c2l(pi.y[locy + 1]) : 0);

return [xa.c2p(pi.x[locx], true),
ya.c2p((1 - dy) * pi.y[locy] + dy * pi.y[locy + 1], true),
ya.c2p(ya.l2c(dyl), true),
locx, locy + dy];
}
}
Binary file modified test/image/baselines/contour_log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions test/image/mocks/contour_nonlinear_interpolation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"data": [
{
"x": [1,2,3,4,5],
"y": [1,2,3,4,5],
"z": [
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
],
"contours": {"start": 10, "end": 90, "size": 10},
"type": "contour"
},
{
"x": [1,10,100,1000,10000],
"y": [1,2,3,4,5],
"z": [
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
],
"contours": {"start": 10, "end": 90, "size": 10},
"type": "contour",
"showscale": false,
"xaxis": "x2"
},
{
"x": [1,2,3,4,5],
"y": [1,10,100,1000,10000],
"z": [
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
],
"contours": {"start": 10, "end": 90, "size": 10},
"type": "contour",
"showscale": false,
"yaxis": "y2"
},
{
"x": [1,10,100,1000,10000],
"y": [1,10,100,1000,10000],
"z": [
[0 , 3.1250, 12.5000, 28.1250, 50.0000],
[3.1250, 6.2500, 15.6250, 31.2500, 53.1250],
[12.5000, 15.6250, 25.0000, 40.6250, 62.5000],
[28.1250, 31.2500, 40.6250, 56.2500, 78.1250],
[50.0000, 53.1250, 62.5000, 78.1250, 100.0000]
],
"contours": {"start": 10, "end": 90, "size": 10},
"type": "contour",
"showscale": false,
"xaxis": "x2",
"yaxis": "y2"
}
],
"layout": {
"xaxis": {"domain": [0, 0.45]},
"xaxis2": {"domain": [0.55, 1], "type": "log"},
"yaxis": {"domain": [0, 0.45]},
"yaxis2": {"domain": [0.55, 1], "type": "log"},
"margin": {"l": 50, "b": 50, "r": 110, "t": 10},
"width": 500,
"height": 400
}
}

0 comments on commit aef563b

Please sign in to comment.