8000 Fix the way to get xs length in set_3d_properties() by wangzexi · Pull Request #20555 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

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

Merged
merged 2 commits into from
Jul 19, 2021

Conversation

wangzexi
Copy link
Contributor
@wangzexi wangzexi commented Jun 30, 2021

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.
Copy link
@github-actions github-actions bot left a comment

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
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
Copy link
Contributor Author
wangzexi commented Jul 1, 2021

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
Copy link
Member
jklymak commented Jul 8, 2021

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
Copy link
Member
timhoffm commented Jul 9, 2021

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
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
0