Allow use of unitful data in images · Issue #19476 · matplotlib/matplotlib · GitHub
Skip to content

Allow use of unitful data in images #19476

Description

@dstansby

Problem

Often the data stored in images can have physical values associated with them. It would therefore be good if there was support for using the unit machinery to allow images to have units.

Proposed Solution

Here's a short example that should work fine using units, but which currently fails. The use of datetime is a bit contrived, but I chose it to avoid a third party package. For a more realistic real-world example see astropy/astropy#11306.

from datetime import datetime
import matplotlib.colors as mcolor
import matplotlib.pyplot as plt

data = [[datetime.now(), datetime.now()],
        [datetime.now(), datetime.now()]]

fig, ax = plt.subplots()
im = ax.imshow(data, norm=mcolor.LogNorm())
fig.colorbar(im)

plt.show()

This fails in

  File "/Users/dstansby/github/matplotlib/lib/matplotlib/axes/_axes.py", line 5586, in imshow
    im.set_data(X)
  File "/Users/dstansby/github/matplotlib/lib/matplotlib/image.py", line 700, in set_data
    raise TypeError("Image data of dtype {} cannot be converted to "
TypeError: Image data of dtype object cannot be converted to float

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions