Skip to content

Allow use of unitful data in images #19476

Open

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

  1. jklymak commented on Feb 8, 2021

    @jklymak
    Member

    Crossref #7383 and #11422 where this is discussed....

  2. jklymak commented on Feb 8, 2021

    @jklymak
    Member

    Re-engineering data handling to make this possible in general is the goal of the Chan-Zuckerberg proposal that just was renewed. I'm not sure if something practical like this should be done in the more immediate term. Adding a converter to everything that could use a converter is possible, but will likely end up causing some pain at somepoitn without some proper engineering. ie. suppose I want to change the z-data of the image in an animation?

  3. dstansby commented on Feb 8, 2021

    @dstansby
    MemberAuthor

    Ah apologies, I didn't find #7383 in my searches before opening this issue. Happy to close this and divert discussion there.

  4. jklymak commented on Feb 8, 2021

    @jklymak
    Member

    Well that discussion was just for categorical, so pointing out that it is more general is useful.

  5. github-actions commented on Oct 6, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  6. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Oct 6, 2023
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