You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Imshow does not copy data array but determines colormap values upon call #6419
Please note that if we comment Z[50]=100 the issue is not apparent as an all white figure is created as expected, but e.g. in Qt4Agg backend, mouseover values shows the altered Z values.
This means that imshow does not copy the data array but sets the color scale at the ax.imshow() call.
If data is intentionally not copied, I'd suggest adding a big warning about this behavior in the documentation and then the colorscale determination should be deferred until the figure is actually drawn. Otherwise the data should be copied.
_AxesImageBase.set_data simply passes the input array through cbook.safe_masked_invalid, which explicitly does not copy. I don't remember whether at some point, perhaps long ago, there was a decision to save time and memory by avoiding the copy, or whether this is just accidental. In any case, maybe the thing to do is add a 'copy' kwarg to safe_masked_invalid and then change its first line to use that flag in a call to array(..., subok=True, ...) in place of the present asanyarray.
To prevent the problem illustrated here, it is essential to copy Z in imshow, as I proposed above. The only remaining question is whether this means we can remove a copy somewhere else, on the grounds that now we know we are safe. We tend to view the question of copying from the standpoint that we don't want mpl to modify an array that is referenced from outside mpl; but the issue here is from a different standpoint, that we don't want an array used inside mpl to be modified from outside.
Activity
tacaswell commentedon May 13, 2016
Data should be copied.
efiring commentedon May 13, 2016
_AxesImageBase.set_datasimply passes the input array throughcbook.safe_masked_invalid, which explicitly does not copy. I don't remember whether at some point, perhaps long ago, there was a decision to save time and memory by avoiding the copy, or whether this is just accidental. In any case, maybe the thing to do is add a 'copy' kwarg tosafe_masked_invalidand then change its first line to use that flag in a call toarray(..., subok=True, ...)in place of the presentasanyarray.I can make a PR for that today.
tacaswell commentedon May 13, 2016
If you throw a
fig.canvas.draw()between callingimshowand changingZit works correctly. We are deferring some operation a bit too long.efiring commentedon May 13, 2016
To prevent the problem illustrated here, it is essential to copy
Zinimshow, as I proposed above. The only remaining question is whether this means we can remove a copy somewhere else, on the grounds that now we know we are safe. We tend to view the question of copying from the standpoint that we don't want mpl to modify an array that is referenced from outside mpl; but the issue here is from a different standpoint, that we don't want an array used inside mpl to be modified from outside.BUG: copy image data so it can't be modified prior to drawing
efiring commentedon May 15, 2016
Closed by #6422
BUG: copy image data so it can't be modified prior to drawing