The Wayback Machine - https://web.archive.org/web/20211130175109/https://github.com/matplotlib/matplotlib/issues/21147
Skip to content
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

[Bug]: Line2D should copy its inputs #21147

Closed
anntzer opened this issue Sep 21, 2021 · 3 comments
Closed

[Bug]: Line2D should copy its inputs #21147

anntzer opened this issue Sep 21, 2021 · 3 comments

Comments

@anntzer
Copy link
Contributor

@anntzer anntzer commented Sep 21, 2021

Bug summary

Currently, Line2D doesn't copy its inputs if they are already arrays. Most of the time, in-place modifications to the input arrays do not affect the draw line, because there is a cache that doesn't get invalidated, but in some circumstances, it is possible for these modifications to affect the drawn line.

Instead, Line2D should just copy its inputs. This was rejected in #736 on a memory-saving argument, but note that AxesImage (which would typically have much bigger (2D) inputs than Line2D (which has 1D inputs)) does a copy, which if anything is much worse memory-wise.

Code for reproduction

from pylab import *
t = arange(0, 6, 2)
l, = plot(t, t, ".-")
savefig("/tmp/1.png")
t[:] = range(3)  # in place change
savefig("/tmp/2.png")  # no effect
l.set_drawstyle("steps")  # ... unless we trigger a cache invalidation
savefig("/tmp/3.png")  # in fact, only the x array got updated, not the y

Actual outcome

(1)
1
(2) (same as (1))
2
(3) (different, but only x got updated, not y)
3

Expected outcome

Modifying t a posteriori should not affect the Line2D. Compare e.g. with AxesImage:

im = arange(9).reshape(3, 3)
imshow(im)
savefig("/tmp/4.png")
im[:, :] = im[::-1, ::-1]
savefig("/tmp/5.png")

Both images are identical.

Operating system

linux

Matplotlib Version

3.5b1

Matplotlib Backend

mplcairo

Python version

39

Jupyter version

No response

Other libraries

No response

Installation

source

Conda channel

No response

@timhoffm
Copy link
Member

@timhoffm timhoffm commented Sep 22, 2021

I agree, for most practical purposes, the memory consumption should be negligable.

If one wanted to be on the safe side, one could add a flag, but I tend to think that's not neccesary.

Loading

@aitikgupta
Copy link
Contributor

@aitikgupta aitikgupta commented Oct 2, 2021

Seems like a well defined what-to-do (with a lot of examples at other places in the code) -- adding it as a good first issue/hacktoberfest-accepted

Loading

@pedabraham
Copy link

@pedabraham pedabraham commented Oct 4, 2021

Hi 🙋‍♂️ I would like to see if I can solve the problem.
Just to make sure that I understood the expected outcome in the example. Should the y be ending in 2, right?

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants