Closed
Description
Problem
Recently I wanted to draw some colormapped vlines. As LineCollection inherits from ScalarMappable, one can indeed write e.g.
from pylab import *
xs = vals = np.arange(10)
plt.vlines(xs, 0, 1, array=vals)
plt.show()
Unfortunately,
- 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. doingvals[5] = 45
after the call tovlines
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...)
Additional context and prior art
Behave like imshow
.