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

Interpolate contours in linear space, not data space #5985

Merged
merged 10 commits into from
Oct 19, 2021

Conversation

andrew-matteson
Copy link
Contributor

Fixes #5899

Contours are determined by interpolation. When the x or y axis is non-linear, the interpolation should be non-linear.

This PR changes calculates a linearized dx and dy, then uses then converts back into pixel space. I've added a test to the dashboard of contour plots of 4 sparsely sampled "cones" that should all have the same fill colors/shapes if interpolation is handled correctly.

@archmoj archmoj added status: reviewable community community contribution labels Oct 18, 2021
@archmoj
Copy link
Contributor

archmoj commented Oct 18, 2021

Thanks very much for the PR.
Now that #5986 merged,
would you please fetch upstream/master then merge it into this branch so that we get all green tests?

@archmoj
Copy link
Contributor

archmoj commented Oct 18, 2021

@alexcjohnson I think the way contours are working on master is actually correct.
Here is a codepen.
If so this PR should add a new feature?

@alexcjohnson
Copy link
Collaborator

"correct" is a bit open to interpretation when it comes to interpolation, but I think what @andrew-matteson has implemented here is more useful. If you've displayed your data on a log axis, the implication is that variations tend to be smoother on a log scale, hence that's the space in which it makes sense to interpolate it.

For reference here's what the new mock would look like with the existing interpolation:
Screen Shot 2021-10-18 at 11 52 15 AM

Pretty obviously worse than the version added in this PR, I'd say. @archmoj do you have a case in mind where the existing interpolation is preferable?

@alexcjohnson
Copy link
Collaborator

Also closer to what we already do with smoothed heatmaps (the same mock after Plotly.restyle(gd,{'type':'heatmap', 'zsmooth':'best'}) - the four variants look slightly different but I think this is actually a symptom of some funny and potentially incorrect things we're doing with extending beyond the data values to make the outer edges of the heatmap bricks - we may want to adapt this for log axes too, especially as this results in us dropping the first row/column in this case!):
Screen Shot 2021-10-18 at 12 06 13 PM

@archmoj
Copy link
Contributor

archmoj commented Oct 18, 2021

@archmoj do you have a case in mind where the existing interpolation is preferable?

Here is a codepen to clarify why I think the existing interpolation looks more accurate. The point in the middle (presented by scatter point) should stay on the correct (middle) level. No?

@alexcjohnson
Copy link
Collaborator

The point in the middle (presented by scatter point) should stay on the correct (middle) level. No?

You could ask the same question about lines connecting points on a scatter plot - we should connect them with a straight line in data space, then transform that line to log axes where it would be a curve. But that's not what we do: even with log axes we connect with a straight line in pixel space (or, if you've selected spline shape, with a bezier curve calculated in pixel space) and I think displaying the data on log axes is a way of saying that, in general, that's also the space you should be interpolating in. This PR is the equivalent for contours. Perhaps at some point the current behavior could be provided as an option, but to me this PR seems a much better default behavior.

4 contour plots of a sparsely sampled cone with x/y chosen so the plot contours appear the same.
revert short circuit of `ya.c2p(ya.l2c(...))` as `ya.l2p(...)` because it breaks carpet contours
@archmoj
Copy link
Contributor

archmoj commented Oct 18, 2021

The point in the middle (presented by scatter point) should stay on the correct (middle) level. No?

You could ask the same question about lines connecting points on a scatter plot - we should connect them with a straight line in data space, then transform that line to log axes where it would be a curve. But that's not what we do: even with log axes we connect with a straight line in pixel space (or, if you've selected spline shape, with a bezier curve calculated in pixel space) and I think displaying the data on log axes is a way of saying that, in general, that's also the space you should be interpolating in. This PR is the equivalent for contours. Perhaps at some point the current behavior could be provided as an option, but to me this PR seems a much better default behavior.

I sort of agree with this part displaying the data on log axes is a way of saying that, in general, that's also the space you should be interpolating in and I won't protest too much if we want to consider this PR a bug fix :)
Interestingly there is a similar issue in 3D I opened a while ago: #3312.
Perhaps we could/should simply close that one if we want to follow this path.

draftlogs/5985_fix.md Outdated Show resolved Hide resolved
@@ -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!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we rename this file to 5985_change.md so that it clearly show up in the Changed section.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense to me - this PR is somewhere between a fix and a change, so let's put it in the somewhat more prominent place (and release it in a minor rather than a patch)

Copy link
Collaborator

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 I'd say this is ready once the changelog entry is moved to change - thanks @andrew-matteson!

FYI I created #5991 to track the heatmap brick edge issue I noted in my earlier comment.

@archmoj archmoj merged commit aef563b into plotly:master Oct 19, 2021
@archmoj archmoj added this to the v2.6.0 milestone Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken community community contribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Contour plot interpolation method always treats data as linear
3 participants