Skip to content

Fix the way to get xs length in set_3d_properties() - #20555

Merged
jklymak merged 2 commits into
matplotlib:masterfrom
wangzexi:master
Jul 19, 2021
Merged

jklymak merged 2 commits into
matplotlib:masterfrom
wangzexi:master

Conversation

@wangzexi

@wangzexi wangzexi commented Jun 30, 2021 •

Copy link
Copy Markdown
Contributor

The xs is a 1D array that may not have a shape attribute.

lines_3d.set_xdata([0, 1])
lines_3d.set_ydata([0, 1])
lines_3d.set_3d_properties([0, 1], zdir='z') # 💥 AttributeError: 'list' object has no attribute 'shape'

The `xs` is a 1D array that may not have a `shape` attribute.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

@story645 story645 added PR: bugfix Pull requests that fix identified bugs topic: mplot3d labels Jun 30, 2021
@tacaswell tacaswell added this to the v3.5.0 milestone Jun 30, 2021
@tacaswell

Copy link
Copy Markdown
Member

Thanks for working on this. This definitively should get a test so we do not regress this in the future.


Are we sure that this is the right place to fix this? I suspect so as we want to preserve the xdata as a list as long as possible?

It looks like this use of .shape came in via 5a49f09 relatively recently. Are we sure that this value will always be 1D (but sometime a list and sometimes an array)?

@wangzexi

wangzexi commented Jul 1, 2021 •

Copy link
Copy Markdown
Contributor Author

Yes, I'm sure.


The Line3D is derived from Line2D, it shows the x will always be a 1D array. For a 1D array, whether its type is list or numpy.ndarray, the len() will always work well.

For np.broadcast_to, the second parameter shape needs a tuple. After testing, both len(xs) or (len(xs),) are working properly.

def set_xdata(self, x):
"""
Set the data array for x.
Parameters
----------
x : 1D array
"""
self._xorig = x
self._invalidx = True
self.stale = True

def get_xdata(self, orig=True):
"""
Return the xdata.
If *orig* is *True*, return the original data, else the
processed data.
"""
if orig:
return self._xorig
if self._invalidx:
self.recache()
return self._x

def set_3d_properties(self, zs=0, zdir='z'):
xs = self.get_xdata()
ys = self.get_ydata()
zs = np.broadcast_to(zs, xs.shape)
self._verts3d = juggle_axes(xs, ys, zs, zdir)
self.stale = True

https://numpy.org/doc/stable/reference/generated/numpy.broadcast_to.html

@jklymak
jklymak marked this pull request as draft July 8, 2021 15:08
@jklymak

jklymak commented Jul 8, 2021

Copy link
Copy Markdown
Member

Please move out of drafts and remove the "needs tests" when the tests are added. Thanks!

@wangzexi
wangzexi marked this pull request as ready for review July 9, 2021 09:30
@timhoffm

timhoffm commented Jul 9, 2021

Copy link
Copy Markdown
Member

Re tuple vs. int:
int support is implemented, but not documented. Let's briefly wait and see if they accept this in their documentation (numpy/numpy#19445). If not, it's considered an implementation detail and we shouldn't use it.

@timhoffm

Copy link
Copy Markdown
Member

Now, it's official: broadcast_to supports int as shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: bugfix Pull requests that fix identified bugs topic: mplot3d

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants