Repository navigation
Description
Bug summary
Line3D.set_data_3d documents its parameters as array-like and accepts anything iterable, storing the arguments verbatim in _verts3d. Line3D.draw then reads self._verts3d[0].shape, so a list, which is array-like and passes the setter's own check, raises AttributeError during draw.
The .shape access lives inside if np.any(scale_mask):, so it is only reached when a coordinate is invalid for the current axis scale. A finite list draws fine, which is why this can sit unnoticed for a long time and then surface the first time a NaN appears.
This is new in 3.11. 3.10.9 has no such branch and draws the same input.
Code for reproduction
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
(line,) = ax.plot([0.0], [0.0], [0.0])
line.set_data_3d([float("nan")], [float("nan")], [float("nan")])
fig.canvas.draw()Actual outcome
Traceback (most recent call last):
...
File ".../mpl_toolkits/mplot3d/art3d.py", line 363, in draw
(len(self._verts3d), *self._verts3d[0].shape)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'shape'
Expected outcome
The figure draws, as it does on 3.10.9 and as it does on 3.11.1 when the same values are passed as arrays:
import numpy as np
line.set_data_3d(np.array([np.nan]), np.array([np.nan]), np.array([np.nan]))
fig.canvas.draw() # fineAdditional information
The two sides disagree about what _verts3d holds.
set_data_3d checks only that each argument is iterable and stores them as given:
for name, xyz in zip('xyz', args):
if not np.iterable(xyz):
raise RuntimeError(f'{name} must be a sequence')
self._verts3d = argsdraw requires them to have .shape:
if np.any(scale_mask):
mask = np.broadcast_to(
scale_mask,
(len(self._verts3d), *self._verts3d[0].shape)
)Behaviour by input and version:
| coordinates | _verts3d holds |
3.10.9 | 3.11.1 |
|---|---|---|---|
| finite | lists | draws | draws |
| NaN | lists | draws | AttributeError |
| NaN | arrays | draws | draws |
The narrowest fix is np.shape(self._verts3d[0]) in draw, which leaves what get_data_3d() returns unchanged. Converting in the setter also works but changes the stored type, so a caller that reads back what it passed would see something different. I have both verified locally and am happy to open a PR for the first.
Worth noting because it is how this was found: #22308 recommended set_data([a], [b]) followed by set_3d_properties([c]) as the way to update a Line3D, which produces exactly this state. Code written against that advice draws on 3.10 and raises on 3.11 as soon as a coordinate goes non-finite.
Operating system
Linux (Debian 13, kernel 6.12)
Matplotlib Version
3.11.1 (3.10.9 unaffected; the branch is present on main)
Matplotlib Backend
Agg
Python version
3.14
Jupyter version
N/A
Installation
pip
Activity
thc1006 commentedon Jul 28, 2026
Some more digging, since "new in 3.11" was not a precise enough description of where this came from or how wide it is.
Where the requirement came from
It arrived in two steps, and the second is what made it reachable by default.
d18c52b "fix Line3D masking" (Jan 2025) reworked
Line3D.drawto build a mask and broadcast it, which is where_verts3d[0].shapefirst appears:At that point the whole block sat behind
if self._axlim_clip:, an opt-in argument, so the new requirement only applied to lines that had asked for clipping.629d396 "Ignore data on 3D plots that are outside the valid axis scale range" then added
_scale_invalid_mask, which runs unconditionally, and changed the guard:So the
.shapeaccess moved from behind an opt-in flag to behind "any coordinate is invalid for its scale". Nothing aboutset_data_3dchanged; the block that consumes what it stores did.Worth noting that
_scale_invalid_maskdocuments its own parameters asarray-like, and the caller then requires.shapeon the same values.3.10.9 has neither:
_viewlim_maskreturned the masked coordinates directly and there was no broadcast. All the cases below draw there.What actually triggers it
Not only NaN. Anything that makes the mask truthy, on 3.11.1 with
_verts3dholding lists:AttributeErrorAttributeError<= 0on a log scaleAttributeErroraxlim_clip=TrueAttributeErrorThe third row is the one I would highlight.
_scale_invalid_maskwas added so that 3D data outside a scale's valid domain is ignored rather than breaking the plot. On aLine3Dwhose coordinates were set as lists, it raises instead, so the feature does not survive its own motivating case.Tuples fail the same way, with
'tuple' object has no attribute 'shape'.How wide
I went through every user of
_scale_invalid_mask/_viewlim_maskinart3d.pyand exercised each through its public entry point with list input and a triggering coordinate:Line3Dset_data_3d(list)Line3Dset_data(list)+set_3d_properties(list)Line3Dax.plot(list)Line3DCollectionLine3DCollection(list of lists)Poly3DCollectionPoly3DCollection(list verts)Path3DCollectionax.scatter(list)Text3Dax.text(nan, ...)Axes3Dplot_surfacewith NaNEverything that goes through a high-level API converts to arrays on the way in, which is why this is not more visible. The two
Line3Dsetters are the exception: they are the documented way to update an existing 3D line in place, and they store what they are handed.Path3DCollection.do_3d_projectionhas the identicalself._offsets3d[0].shapeone screen down. Assigning_offsets3da tuple of lists directly reproduces the same error, but that attribute is private and its public writer routes throughjuggle_axeswithget_offsets(), so I have not counted it as a public-API break. Happy to include it in #32128 if you would rather the two were consistent.Fix
#32128 uses
np.shape(self._verts3d[0]), which accepts both and leaves whatget_data_3d()returns unchanged. Verified against a released 3.11.1 with onlyart3d.pyswapped: the new test fails before and passes after, and the rest oftest_art3d.pystays green.mohit-sharma-001 commentedon Jul 29, 2026
@thc1006 i would like to view this issue more
I am willing to take this issue
thc1006 commentedon Jul 29, 2026
I am doing now u can see already have a PR.
ShamikOfficial commentedon Sep 5, 2026
Working on a fix for this — coercing the coordinates to arrays in
set_data_3dsodrawcan safely use.shape.chirayu08 commentedon Sep 8, 2026
Use this change in mpl_toolkits/mplot3d/art3d.py:
if np.any(scale_mask):
mask = np.broadcast_to(
scale_mask,
(len(self._verts3d), *np.shape(self._verts3d[0]))
)
The important part is changing:
self._verts3d[0].shape
to:
np.shape(self._verts3d[0])
Why this is the better fix
It preserves the existing behavior of:
line.get_data_3d()
because you're not changing what set_data_3d() stores.
You're simply making draw() consistent with the setter's documented support for array-like/iterable inputs.
Before opening the PR
I'd add a regression test. Look for the existing Line3D tests, probably under something like:
lib/mpl_toolkits/mplot3d/tests/
Search for:
set_data_3d
Then add a test covering the exact failure:
line.set_data_3d([np.nan], [np.nan], [np.nan])
fig.canvas.draw()
Ideally test lists, because that's the regression:
def test_line3d_set_data_list_with_nan():
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
(line,) = ax.plot([0.0], [0.0], [0.0])
Follow the existing Matplotlib test style rather than blindly copying this exact test.
Then run the relevant tests
From the Matplotlib repository:
python -m pytest lib/mpl_toolkits/mplot3d/tests/
If that's too broad, first locate the relevant test file and run just that file:
python -m pytest path/to/test_file.py
Then run the specific test:
python -m pytest path/to/test_file.py -k line3d
Your PR should be small
Ideally your PR contains only:
art3d.py
↓
np.shape(...) fix
test file
↓
regression test
Don't modify unrelated formatting or refactor other code.