Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: size and color rendering for Path3DCollection #19812

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 1 commit into from
Mar 31, 2021

Conversation

tacaswell
Copy link
Member

PR Summary

This fixes both bugs identified in #19787

Still needs test and the tactic should be expanded to the other *Collections before we merge.

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Mar 29, 2021
@tacaswell tacaswell added this to the v3.4.1 milestone Mar 29, 2021
@tacaswell tacaswell marked this pull request as ready for review March 30, 2021 22:42
@jklymak
Copy link
Member

jklymak commented Mar 31, 2021

Looks like a real doc-build failure.... :-(

generating gallery for gallery/mplot3d... [  2%] 2dcollections3d.py
WARNING: /home/circleci/project/examples/mplot3d/2dcollections3d.py failed to execute correctly: Traceback (most recent call last):
  File "/home/circleci/project/examples/mplot3d/2dcollections3d.py", line 36, in <module>
    ax.legend()
  File "/home/circleci/project/lib/matplotlib/axes/_axes.py", line 290, in legend
    self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
  File "/home/circleci/project/lib/matplotlib/legend.py", line 503, in __init__
    self._init_legend_box(handles, labels, markerfirst)
  File "/home/circleci/project/lib/matplotlib/legend.py", line 766, in _init_legend_box
    handle_list.append(handler.legend_artist(self, orig_handle,
  File "/home/circleci/project/lib/matplotlib/legend_handler.py", line 115, in legend_artist
    artists = self.create_artists(legend, orig_handle,
  File "/home/circleci/project/lib/matplotlib/legend_handler.py", line 447, in create_artists
    p = self.create_collection(orig_handle, sizes,
  File "/home/circleci/project/lib/matplotlib/legend_handler.py", line 459, in create_collection
    p = type(orig_handle)([orig_handle.get_paths()[0]],
  File "/home/circleci/project/lib/mpl_toolkits/mplot3d/art3d.py", line 525, in __init__
    super().__init__(*args, **kwargs)
  File "/home/circleci/project/lib/matplotlib/collections.py", line 1029, in __init__
    super().__init__(**kwargs)
  File "/home/circleci/project/lib/matplotlib/_api/deprecation.py", line 421, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/home/circleci/project/lib/matplotlib/collections.py", line 180, in __init__
    self.set_linewidth(linewidths)
  File "/home/circleci/project/lib/mpl_toolkits/mplot3d/art3d.py", line 570, in set_linewidth
    if not self._in_draw:
AttributeError: 'Path3DCollection' object has no attribute '_in_draw'

@tacaswell
Copy link
Member Author

I was thinking about the fix @QuLogic put in for #18916 and was wondering if that was going to come back in other ways and it did!

@tacaswell
Copy link
Member Author

No, I jumped to conclusion, but it is an easy fix. Running a full docs build locally before pushing.

@QuLogic
Copy link
Member

QuLogic commented Mar 31, 2021

I think you're running into the same issue as #18932 did, namely that legend copies the class, and so creates a 3D collection for its legend handle, without doing any of the 3D work.

@tacaswell
Copy link
Member Author

I think this is slightly different, the problem is it is actually creating a the 3D class, rather than the slightly-upsetting

def patch_collection_2d_to_3d(col, zs=0, zdir='z', depthshade=True):
"""
Convert a :class:`~matplotlib.collections.PatchCollection` into a
:class:`Patch3DCollection` object
(or a :class:`~matplotlib.collections.PathCollection` into a
:class:`Path3DCollection` object).
Parameters
----------
za
The location or locations to place the patches in the collection along
the *zdir* axis. Default: 0.
zdir
The axis in which to place the patches. Default: "z".
depthshade
Whether to shade the patches to give a sense of depth. Default: *True*.
"""
if isinstance(col, PathCollection):
col.__class__ = Path3DCollection
elif isinstance(col, PatchCollection):
col.__class__ = Patch3DCollection
col._depthshade = depthshade
col.set_3d_properties(zs, zdir)
so in the case of scatter we were creating it via the 3d props call, but in the scatter case we were running the __init__ which bounced to the super class which tries to set the linewidth before setup the 3D stuff.

@tacaswell
Copy link
Member Author

Going to make an executive decision and say that @QuLogic can merge this on one review so we can get 3.4.1 out.

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

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

I have minor comments, and I'll leave this open for a little bit in case anyone else is able to review.

This fixes matplotlib#19787

This modifies the way we manage internal depth sorting of elements in the 3D
collections.  We need to both preserve the original order (so we can keep the
position, size, colour, ... in sync), but present each of them to the 2D
rendering code in depth order (back to front).  In some cases we can handle
this by sorting the values in the getter methods and in others we have to keep
a cache of both the sorted (the names used by the 2D code) and
unsorted (post-fixed with 3d) data.

It does not appear that Line3DCollection does any of the z-sorting that the
other collections do.  While definitely a limitation and produces MC Escher
effects, this is out of scope of the current work.

The z-sorting on Poly3DCollection is significantly more sophisticated than the
sorting on Path3DCollection and Patch3DCollection so it is handled
differently.

Co-authored-by: Elliott Sales de Andrade <[email protected]>
@QuLogic QuLogic merged commit ee9500a into matplotlib:master Mar 31, 2021
QuLogic added a commit to QuLogic/matplotlib that referenced this pull request Mar 31, 2021
QuLogic added a commit that referenced this pull request Mar 31, 2021
Backport PR #19812: FIX: size and color rendering for Path3DCollection
@tacaswell tacaswell deleted the fix_scatter3D branch March 31, 2021 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants