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

Skip to content

Patch3DCollection not displaying correctly using add_collection3d #14298

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

Open
ghost opened this issue May 22, 2019 · 4 comments
Open

Patch3DCollection not displaying correctly using add_collection3d #14298

ghost opened this issue May 22, 2019 · 4 comments

Comments

@ghost
Copy link

ghost commented May 22, 2019

Code for reproduction

import matplotlib as mpl
import matplotlib.pyplot
import mpl_toolkits as mplt
import mpl_toolkits.mplot3d.axes3d
fig = mpl.pyplot.figure() # make Figure
ax = mpl.pyplot.gca(projection="3d")
circle1 = mpl.patches.Circle((5,1))
circle2 = mpl.patches.Circle((2,2))

# test 1 (throws ValueError):
#pc1 = matplotlib.collections.PatchCollection([circle1, circle2])
#ax.add_collection3d(pc1, zs=[1,2])

# test 2:
pc2 = mplt.mplot3d.art3d.Patch3DCollection([circle1, circle2], match_original=True)
ax.add_collection3d(pc2)

test 1 results into a ValueError:

Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
    self.draw()
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/matplotlib/figure.py", line 1649, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 287, in draw
    reverse=True)):
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 286, in <lambda>
    key=lambda col: col.do_3d_projection(renderer),
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 400, in do_3d_projection
    fcs = (zalpha(self._facecolor3d, vzs) if self._depthshade else
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 776, in zalpha
    colors = get_colors(colors, len(zs))
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 767, in get_colors
    (num, 4))
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 176, in broadcast_to
    return _broadcast_to(array, shape, subok=subok, readonly=True)
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 128, in _broadcast_to
    op_flags=[op_flag], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,4) and requested shape (1,4)
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
    self.draw()
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/matplotlib/figure.py", line 1649, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 287, in draw
    reverse=True)):
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 286, in <lambda>
    key=lambda col: col.do_3d_projection(renderer),
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 400, in do_3d_projection
    fcs = (zalpha(self._facecolor3d, vzs) if self._depthshade else
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 776, in zalpha
    colors = get_colors(colors, len(zs))
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 767, in get_colors
    (num, 4))
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 176, in broadcast_to
    return _broadcast_to(array, shape, subok=subok, readonly=True)
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 128, in _broadcast_to
    op_flags=[op_flag], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,4) and requested shape (1,4)
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py", line 519, in _draw_idle
    self.draw()
  File "/usr/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 402, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/matplotlib/figure.py", line 1649, in draw
    renderer, self, artists, self.suppressComposite)
  File "/usr/lib/python3.6/site-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "/usr/lib/python3.6/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 287, in draw
    reverse=True)):
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 286, in <lambda>
    key=lambda col: col.do_3d_projection(renderer),
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 400, in do_3d_projection
    fcs = (zalpha(self._facecolor3d, vzs) if self._depthshade else
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 776, in zalpha
    colors = get_colors(colors, len(zs))
  File "/usr/lib/python3.6/site-packages/mpl_toolkits/mplot3d/art3d.py", line 767, in get_colors
    (num, 4))
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 176, in broadcast_to
    return _broadcast_to(array, shape, subok=subok, readonly=True)
  File "/usr/lib/python3.6/site-packages/numpy/lib/stride_tricks.py", line 128, in _broadcast_to
    op_flags=[op_flag], itershape=shape, order='C')
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,4) and requested shape (1,4)

test 2 results into a weird plot drenched into a blueish color, there is a visible object also (perhaps change the viewing angle) but it's not a circle.
Figure_1

Matplotlib 3.1.0

  • Operating system: Void Linux
  • Matplotlib version: 3.1.0
  • Matplotlib backend Qt5Agg:
  • Python version: 3.6.8
@fidelram
Copy link

I am also getting the same error when saving a 3D image.

@ivirshup
Copy link

@QuLogic, was this fixed by #18929? The plots at scanpy seem to be working with matplotlib 3.3.3.

@qrzhang
Copy link

qrzhang commented Nov 17, 2020

@ivirshup Yes, I think you're right. It works after I upgrade the matplotlib to version 3.3.3

pip install matplotlib --upgrade

@oscargus
Copy link
Member

Parts of this seems to be related to scaling of the patches. Consider the following code:

import matplotlib as mpl
import matplotlib.pyplot
fig = mpl.pyplot.figure() # make Figure
ax = fig.add_subplot(projection="3d")
rect = mpl.patches.Rectangle((-0.05,-0.05), 0.1, 0.1)
circle1 = mpl.patches.Circle((0,0), radius=0.01)
circle2 = mpl.patches.Circle((0.01,0.01), radius=0.01)
ax.set_xlim(-10, 10)
ax.set_ylim(-10, 10)
ax.set_zlim(-10, 10)
pc1 = matplotlib.collections.PatchCollection([rect, circle1, circle2])
ax.add_collection3d(pc1, zs=[1,2,3])
#pc2 = mplt.mplot3d.art3d.Patch3DCollection([rect, circle1, circle2], zs=[1, 2, 3], match_original=True)
#ax.add_collection3d(pc2)

Either version works, and gives the following plot:
image

@QuLogic QuLogic modified the milestones: v3.6.0, v3.7.0 Aug 24, 2022
@ksunden ksunden modified the milestones: v3.7.0, v3.7.1 Feb 14, 2023
@QuLogic QuLogic modified the milestones: v3.7.1, future releases Mar 4, 2023
@Chrillebon Chrillebon mentioned this issue Aug 8, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants