Skip to content

Patch3DCollection not displaying correctly using add_collection3d  #14298

Closed as not planned

Description

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

Activity

  1. fidelram commented on May 26, 2019

    @fidelram

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

  2. added this to the v3.1.2 milestone on Jul 1, 2019
  3. modified the milestones: v3.1.2, v3.3.0 on Oct 22, 2019
  4. modified the milestones: v3.3.0, v3.4.0 on May 7, 2020
  5. ivirshup commented on Nov 12, 2020

    @ivirshup

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

  6. qrzhang commented on Nov 17, 2020

    @qrzhang

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

    pip install matplotlib --upgrade

  7. modified the milestones: v3.4.0, v3.5.0 on Jan 27, 2021
  8. modified the milestones: v3.5.0, v3.6.0 on Sep 25, 2021
  9. oscargus commented on Jul 20, 2022

    @oscargus
    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

  10. modified the milestones: v3.6.0, v3.7.0 on Aug 24, 2022
  11. modified the milestones: v3.7.0, v3.7.1 on Feb 14, 2023
  12. modified the milestones: v3.7.1, future releases on Mar 4, 2023
  13. github-actions commented on Sep 8, 2025

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  14. added
    status: inactive
    on Sep 8, 2025
  15. added
    status: closed as inactive
    on Oct 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: closed as inactivestatus: inactivetopic: mplot3d

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions