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
ScalarMappable should copy its input and allow non-arrays #18841
The array kwarg only accepts ndarrays, and not e.g. nested lists: plt.vlines(np.arange(10), 0, 1, array=[*range(10)]) crashes (at draw time, which is even worse) with "AttributeError: 'list' object has no attribute 'ndim'". This is unlike most other Axes/pyplot APIs (e.g. imshow), which happily accept lists (nested/2D lists, for imshow).
The array kwarg is not copied, so later changes to it get reflected back into the artist (i.e. doing vals[5] = 45after the call to vlines still affects the line colors). Again, this is unlike other Axes/pyplot APIs (e.g. imshow), which insulate the artist from later changes to the input.
Proposed Solution
Make a copy of the input and cast it to arrays. (Well, modulo unit handling...)
Activity
aitikgupta commentedon Nov 2, 2020
I'd love to take this up! :)
Initial scanning leads to the following points:
imshow,_ImageBaseclass inherits fromScalarMappable, but overrides itsset_arrayfunction as:matplotlib/lib/matplotlib/image.py
Lines 733 to 743 in 59bfcfa
imshowdoesn't raise this error as itsset_data(called fromset_array) copies the original array and checks for numpy casts:matplotlib/lib/matplotlib/image.py
Lines 696 to 701 in 59bfcfa
vlines,ScalarMappableis inherited again, but there's no overriding itsset_arrayfunction, which evidently just acceptsndarray:matplotlib/lib/matplotlib/cm.py
Lines 363 to 372 in 59bfcfa
aitikgupta commentedon Nov 2, 2020
To solve this, I added
set_arraytoCollectionclass, overriding it again. It will need some reviews, I'll issue a PR for the same.