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

Skip to content

[Bug]: AttributeError: 'Path3DCollection' object has no attribute '_offset_zordered' (possible regression) #26497

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

Closed
varkappadev opened this issue Aug 11, 2023 · 6 comments · Fixed by #26719

Comments

@varkappadev
Copy link

Bug summary

Adding a Line3DCollection to an animation results in Attribute error.

Code for reproduction

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.collections import LineCollection
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import numpy as np
import pkg_resources
import sys

lc = [(np.fromiter([0., 0., 0.], dtype="float"),
       np.fromiter([1., 1., 1.], dtype="float"))]
pc = [np.fromiter([0., 0.], dtype="float"), np.fromiter(
    [0., 1.], dtype="float"), np.fromiter([1., 1.], dtype="float")]

print(pkg_resources.get_distribution("matplotlib").version, file=sys.stderr)

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
an = [[ax.add_collection(Line3DCollection(lc)),
       ax.scatter(*pc),
       ax.scatter(*pc),
       ],
      [ax.scatter(*pc),
       ax.scatter(*pc),
       ]
      ]
anim = animation.ArtistAnimation(fig, artists=an)
anim.save("mpl-test.webp", fps=100, writer="pillow")

Actual outcome

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 233, in saving
    yield self
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 1107, in save
    writer.grab_frame(**savefig_kwargs)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 495, in grab_frame
    self.fig.savefig(
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/figure.py", line 3378, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2366, in print_figure
    result = print_method(
             ^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2232, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
                                                                 ^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 445, in print_raw
    FigureCanvasAgg.draw(self)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/backends/backend_agg.py", line 400, in draw
    self.figure.draw(self.renderer)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/figure.py", line 3175, in draw
    mimage._draw_list_compositing_images(
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 492, in draw
    super().draw(renderer)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3064, in draw
    mimage._draw_list_compositing_images(
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/image.py", line 131, in _draw_list_compositing_images
    a.draw(renderer)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/artist.py", line 39, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/mpl_toolkits/mplot3d/art3d.py", line 643, in draw
    with self._use_zordered_offset():
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/mpl_toolkits/mplot3d/art3d.py", line 758, in _use_zordered_offset
    if self._offset_zordered is None:
       ^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Path3DCollection' object has no attribute '_offset_zordered'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/tmp/mpl-test.py", line 27, in <module>
    anim.save("mpl-test.webp", fps=100, writer="pillow")
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 1085, in save
    with mpl.rc_context({'savefig.bbox': None}), \
  File "/opt/homebrew/Cellar/[email protected]/3.11.4_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 235, in saving
    self.finish()
  File "/opt/homebrew/lib/python3.11/site-packages/matplotlib/animation.py", line 501, in finish
    self._frames[0].save(
    ~~~~~~~~~~~~^^^
IndexError: list index out of range

Expected outcome

working animation

Additional information

Affected version: 3.7.2 (macos + homebrew + pip)
Unaffected: 3.6.3 (debian)

I am not sure if it is a backend or version issue. The problem also happens on mac with jupyter notebook and the notebook (inline and widget not tested) backend with my original (non-minimal) code.

Operating system

macos

Matplotlib Version

3.7.2

Matplotlib Backend

MacOSX

Python version

3.11.4

Jupyter version

7.0.2

Installation

pip

@QuLogic
Copy link
Member

QuLogic commented Aug 11, 2023

Bisect points to 94e21bd.

@agnes-sharan
Copy link

Is this open for contribution?

@rcomer
Copy link
Member

rcomer commented Aug 12, 2023

@agnes-sharan anyone is welcome to work on any issue. Please see our Contributors’ Guide.
https://matplotlib.org/devdocs/devel/index.html

@half1red
Copy link

half1red commented Sep 8, 2023

Hi,
I have encountered an even simpler use case that trigger the same error :

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import numpy as np

lc = [
    (
        np.fromiter([0.0, 0.0, 0.0], dtype="float"),
        np.fromiter([1.0, 1.0, 1.0], dtype="float"),
    )
]
pc = [
    np.fromiter([0.0, 0.0], dtype="float"),
    np.fromiter([0.0, 1.0], dtype="float"),
    np.fromiter([1.0, 1.0], dtype="float"),
]

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
lines = ax.add_collection(Line3DCollection(lc))
scatter = ax.scatter(*pc, visible=False)
plt.show()

It seems that it is plotting an invisible element that cause an issue.

However, if the scatter plot is plotted as visible, and then set invisible and the plot draw again, the error doesn't happen:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Line3DCollection
import numpy as np

lc = [
    (
        np.fromiter([0.0, 0.0, 0.0], dtype="float"),
        np.fromiter([1.0, 1.0, 1.0], dtype="float"),
    )
]
pc = [
    np.fromiter([0.0, 0.0], dtype="float"),
    np.fromiter([0.0, 1.0], dtype="float"),
    np.fromiter([1.0, 1.0], dtype="float"),
]

fig = plt.figure()
ax = fig.add_subplot(projection="3d")
lines = ax.add_collection(Line3DCollection(lc))
sc = ax.scatter(*pc)
plt.draw()
plt.pause(0.001)
sc.set_visible(False)
plt.draw()
input("Press [enter] to continue.")

I don't know how to fix this, but I hope that can help.

I am using matplotlib 3.7.2

@oscargus
Copy link
Member

oscargus commented Sep 8, 2023

Thanks @half1red

If nothing else, by bumping this I saw it and was able to fix the bug (and used your example as a test, it also fixes the original issue).

@half1red
Copy link

half1red commented Sep 8, 2023

That's great, happy to hear that, and to help, even so modestly.

@QuLogic QuLogic added this to the v3.7.3 milestone Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants