Skip to content

Fix mplot3d projection #8896

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

Merged
merged 9 commits into from
Feb 7, 2020

Conversation

eric-wieser
Copy link
Contributor

@eric-wieser eric-wieser commented Jul 16, 2017

Fixes #8894 and fixes #1104, replacing the bad rendering in the first image, with the good rendering in the second:

image

image

Unsurprisingly, this breaks every single unit test,

The plots now all look rather cubic - but that's better than distorted, and we could go back to correctly-scaled rectangles with #8593

@@ -985,6 +999,8 @@ def get_proj(self):
point.

"""
pb_aspect = np.array([1, 1, 0.75])
Copy link
Contributor Author

@eric-wieser eric-wieser Jul 16, 2017

Choose a reason for hiding this comment

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

Strictly speaking, this isn't just the aspect, but also the scaling. Is there already scaling in place somewhere else? Seems too convenient that everything just magically fits into the viewing area.

@eric-wieser eric-wieser force-pushed the fix-mplot3d-projection branch 2 times, most recently from 9355ac4 to 5922edd Compare July 16, 2017 22:28
@eric-wieser
Copy link
Contributor Author

Plot is now scaled to sort-of look like it did before, in the default view.

Note that because the plot is now actually stretched horizontally, but the camera is not, the view angle looks different. Options:

  • Change the default view angle too - presumably overriden in matplotlibrc though?
  • Change the default pb_aspect back to a cube
  • Deal with it

@eric-wieser
Copy link
Contributor Author

Reasonably straightforward to implement axis=equal from here too

@tacaswell tacaswell added this to the 2.2 (next next feature release) milestone Jul 18, 2017
# in the superclass, we would go through and actually deal with axis
# scales and box/datalim. Those are all irrelevant - all we need to do
# is make sure our coordinate system is square.
figW, figH = self.get_figure().get_size_inches()
Copy link
Member

Choose a reason for hiding this comment

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

Why use the figure's aspect ratio? What if the figure has 3 subplots all in a row?

Copy link
Contributor Author

@eric-wieser eric-wieser Aug 2, 2017

Choose a reason for hiding this comment

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

Because all sizes in matplotlib are relative to the figure. pb.shrunk_to_aspect seems to take the ratio of the top-level parent (the figure), and the desired ratio. This is because the end result is the size relative to the parent, as all objects in matplotlib seem to be sized - so to ensure a given aspect ratio, you need to account for that of the parent

Note that this is the same as the normal aspect=equal code, so is presumably correct.

Copy link
Member

Choose a reason for hiding this comment

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

Ping @efiring, as I think you are the resident expert on all the ways this could possibly go wrong.

Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense. I have no intention of spending the time required to understand mplot3d well enough to make a detailed review, but in the absence of any such review I am inclined to trust that this PR is a major improvement, as it appears to be fixing a basic design flaw.

@WeatherGod
Copy link
Member

WeatherGod commented Aug 2, 2017 via email

return 'azimuth=%d deg, elevation=%d deg ' % (self.azim, self.elev)
# ignore xd and yd and display angles instead
return 'azimuth={:.0f} deg, elevation={:.0f} deg '.format(
self.azim, self.elev)
Copy link
Contributor Author

@eric-wieser eric-wieser Dec 10, 2017

Choose a reason for hiding this comment

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

Bug fixed here on fractional view angles

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extracted to #12573. I misremembered - the crash was only for non-finite values.

@darkdragon-001
Copy link

Any progress here?

@timhoffm
Copy link
Member

Unfortunately not. Progress would be noted here.

@eric-wieser
Copy link
Contributor Author

I was under the impression the ball was not in my court here. Rebasing shouldn't be too awful, but I don't see much point without further review.

@timhoffm
Copy link
Member

@eric-wieser Not an expert in mpl3d and thus I have no idea who would have to take up the torch. I was just stating that the thread still reflects the current status.

@eric-wieser
Copy link
Contributor Author

@timhoffm: I'll rebase for good measure, it'll allow someone to work from this branch at least if they need cubic plots.

Can you remind me how to regenerate the baseline_images? Is there a mechanism to say "assume the new image is correct for all failed image comparison tests"?

Copy link
Member

@efiring efiring left a comment

Choose a reason for hiding this comment

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

In the interest of getting things moving, I will approve of this subject to the necessary rebase and addition of "what's new" and "API changes" entries. I will defer to @WeatherGod, however, if he has concrete objections. Same with respect to @tacaswell who might object because this is making a big behavior change.

# in the superclass, we would go through and actually deal with axis
# scales and box/datalim. Those are all irrelevant - all we need to do
# is make sure our coordinate system is square.
figW, figH = self.get_figure().get_size_inches()
Copy link
Member

Choose a reason for hiding this comment

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

I think it makes sense. I have no intention of spending the time required to understand mplot3d well enough to make a detailed review, but in the absence of any such review I am inclined to trust that this PR is a major improvement, as it appears to be fixing a basic design flaw.

@timhoffm
Copy link
Member

@eric-wieser
Copy link
Contributor Author

Copy the output images (in this case result_images/test_lines/test_line_dashes.png) to the correct subdirectory of baseline_images tree in the source directory

I was hoping there was automation for "assume every failed output is an intended change, overwrite the baseline" so that I can diff with git.

@jklymak
Copy link
Member

jklymak commented Mar 24, 2019

I was hoping there was automation for "assume every failed output is an intended change, overwrite the baseline" so that I can diff with git.

Can you just rsync result_images/ and lib/matplotlib/tests/baseline_images?

@efiring
Copy link
Member

efiring commented Mar 24, 2019

No, rsync is not the right tool--you need to explicitly copy the appropriate files from result_images, not all of them.

@jklymak
Copy link
Member

jklymak commented Mar 24, 2019

Certainly you dont want to git add all the comparison files.

@anntzer
Copy link
Contributor

anntzer commented Mar 24, 2019

triage_tests.py has keyboard shortcuts (run with -h to get the help) to accept images and move between them ("A"/"down"/"A"/"down"/etc should get you there).

@eric-wieser eric-wieser force-pushed the fix-mplot3d-projection branch from 5922edd to 76a36bb Compare March 26, 2019 05:43
@eric-wieser
Copy link
Contributor Author

@anntzer: Great tool, thanks. Rebased with updated tests

@eric-wieser eric-wieser force-pushed the fix-mplot3d-projection branch 2 times, most recently from 9f8cc4c to a7a8051 Compare March 27, 2019 03:49
@eric-wieser
Copy link
Contributor Author

I don't understand the circle-ci failures:

/home/circleci/project/lib/mpl_toolkits/mplot3d/axes3d.py:docstring of mpl_toolkits.mplot3d.axes3d.Axes3D.apply_aspect:4: WARNING: more than one target found for cross-reference 'get_anchor': matplotlib.axes.Axes.get_anchor, mpl_toolkits.axes_grid1.axes_divider.AxesDivider.get_anchor, mpl_toolkits.axes_grid1.axes_divider.Divider.get_anchor

It seems that travis is failing the svg tests, but the svg files are no longer being generated on my machine.

@eric-wieser
Copy link
Contributor Author

Thanks for the fixups @anntzer, they look fine to me

@eric-wieser
Copy link
Contributor Author

What is the status of this?

@jklymak jklymak requested review from WeatherGod and efiring February 7, 2020 18:01
Copy link
Member

@tacaswell tacaswell left a comment

Choose a reason for hiding this comment

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

Looking through all of the changed images

  • lib/mpl_toolkits/tests/baseline_images/test_mplot3d/bar3d_notshaded.png
  • lib/mpl_toolkits/tests/baseline_images/test_mplot3d/scatter3d_color.png
  • lib/mpl_toolkits/tests/baseline_images/test_mplot3d/scatter3d.png
  • lib/mpl_toolkits/tests/baseline_images/test_mplot3d/surface3d_shaded.png

give me a bit of pause, but I think those are all consequences of the 2.0 default value changes, not due to anything in this PR.

@tacaswell
Copy link
Member

I'm not comfortable merging this over @WeatherGod 's requested changes, however I believe they are addressed.

@eric-wieser
Copy link
Contributor Author

but I think those are all consequences of the 2.0 default value changes, not due to anything in this PR.

I've tried to make that easy to look at by having separate commits for the style and other changes - so hopefully you can confirm whether those changes matter.

@WeatherGod WeatherGod merged commit 62d8d65 into matplotlib:master Feb 7, 2020
Comment on lines +983 to +984
# chosen for similarity with the initial view before gh-8896
pb_aspect = np.array([4, 4, 3]) / 3.5
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I imagine that there are going to be some requests for an API to change this, for people who liked their 1:1:1 plot boxes. I started working on that two years ago, but it opened a can of worms that I didn't want to open concurrently with this PR. At some point I'll see if I can rebase those changes.

@tacaswell
Copy link
Member

Please see https://discourse.matplotlib.org/t/removed-commits-from-master-branch/20868 This merge was "un-done" so that we can re-do it and squash.

@WeatherGod
Copy link
Member

Sorry for causing such churn in the repo. I completely forgot to double-check how many image commits there were.

@eric-wieser
Copy link
Contributor Author

To be clear, is the current status of this "not actually merged"?

@anntzer
Copy link
Contributor

anntzer commented Feb 11, 2020

Yes. I think you can reopen a PR with the same commits, except that the intermediate images should be stripped out (so yes the intermediate commits won't be testable independently).

tacaswell added a commit to tacaswell/matplotlib that referenced this pull request May 27, 2020
To make bbox_inches='tight' work correctly, we cache the positions of
all of the axes, change their aspect to 'auto', adjust the transforms
on the figure, render the output, and then restore the previous
setting of aspect to each of the figures.  This prevent the Axes from
trying to adjust their size on draw. This matters because for the
render that is emitted the figure transforms are out of sync with the
figure size.

As part of cleaning fixing the rendering of Axes3D in matplotlib#8896 / matplotlib#16472
we started to use apply_aspect to re-size the area the Axes3D takes up
to maintain a fixed ratio between the axes when the aspect is "auto".
However this conflicts with the expectation in tight_bbox.adjust_bbox
as it assumes setting the aspect to "auto" will prevent any axes
resizing.

This commit addresses this by:
 - exiting the Axes3D.apply_aspect early if aspect is auto
 - adding a new aspect mode 'auto_pb' which is the default
   for Axes3D which maintains the current master branch behavior
   of maintaining a fixed ratio between the sizes of the x, y z axis
   independent of the data limits.
 - re implement several functions on Axes3D to make sure they handle
   sharez correctly.

closes matplotlib#16463.
tacaswell added a commit to tacaswell/matplotlib that referenced this pull request May 30, 2020
The way that bbox_inches='tight' is implemented we need to ensure that
we do not try to adjust the aspect during the draw (because we have
temporarily de-coupled the reported figure size from the transforms
which results in the being distorted).  Previously we did not have a
way to fix the aspect ratio in screen space of the Axes (only the
aspect ratio in dataspace) however in 3.3 we gained this ability for
both Axes (matplotlib#14917) and Axes3D (matplotlib#8896 / matplotlib#16472).

Rather than add an aspect value to `set_aspect` to handle this case,
in the tight_bbox code we monkey-patch the `apply_aspect` method with
a no-op function and then restore it when we are done.  Previously we
would set the aspect to "auto" and restore it in the same places.

closes matplotlib#16463.
tacaswell added a commit to tacaswell/matplotlib that referenced this pull request Jun 4, 2020
The way that bbox_inches='tight' is implemented we need to ensure that
we do not try to adjust the aspect during the draw (because we have
temporarily de-coupled the reported figure size from the transforms
which results in the being distorted).  Previously we did not have a
way to fix the aspect ratio in screen space of the Axes (only the
aspect ratio in dataspace) however in 3.3 we gained this ability for
both Axes (matplotlib#14917) and Axes3D (matplotlib#8896 / matplotlib#16472).

Rather than add an aspect value to `set_aspect` to handle this case,
in the tight_bbox code we monkey-patch the `apply_aspect` method with
a no-op function and then restore it when we are done.  Previously we
would set the aspect to "auto" and restore it in the same places.

closes matplotlib#16463.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mplot3d projection results in non-orthogonal axes Resizing a GUI window with Axes3D
10 participants