Skip to content

ScalarMappable should copy its input and allow non-arrays #18841

Closed
@anntzer

Description

@anntzer

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()

and get
test

Unfortunately,

  1. 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).
  2. The array kwarg is not copied, so later changes to it get reflected back into the artist (i.e. doing vals[5] = 45 after 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...)

Additional context and prior art

Behave like imshow.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions