Skip to content

Proof of concept images with units #19481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,17 @@ def __init__(self, ax, mappable, **kwargs):
self.mappable = mappable
_add_disjoint_kwargs(kwargs, cmap=mappable.cmap, norm=mappable.norm)

# Set the formatter if
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #18900 for changes in how the formatter and locator will work in 3.5

# - It isn't explicity specified
# - The mappable has a converter and units that provide a formatter
if 'format' not in kwargs:
if hasattr(mappable, 'converter') and hasattr(mappable, 'units'):
info = mappable.converter.axisinfo(mappable.units, self)
if info is None:
pass
if info.majfmt is not None:
kwargs['format'] = info.majfmt

if isinstance(mappable, contour.ContourSet):
cs = mappable
_add_disjoint_kwargs(
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import matplotlib.colors as mcolors
import matplotlib.cm as cm
import matplotlib.cbook as cbook
# Need to import dates to register unit convertors
import matplotlib.dates
import matplotlib.units as munits
# For clarity, names from _image are given explicitly in this module:
import matplotlib._image as _image
# For user convenience, the names from _image are also imported into
Expand Down Expand Up @@ -693,6 +696,7 @@ def set_data(self, A):
"""
if isinstance(A, PIL.Image.Image):
A = pil_to_array(A) # Needed e.g. to apply png palette.
A = self._convert_units(A)
self._A = cbook.safe_masked_invalid(A, copy=True)

if (self._A.dtype != np.uint8 and
Expand Down Expand Up @@ -730,6 +734,16 @@ def set_data(self, A):
self._rgbacache = None
self.stale = True

def _convert_units(self, A):
# Take the first element since units expects a 1D sequence, not 2D
converter = munits.registry.get_converter(A[0])
if converter is None:
return A

self.converter = converter
self.units = converter.default_units(A, self)
return converter.convert(A, self.units, self)

def set_array(self, A):
"""
Retained for backwards compatibility - use set_data instead.
Expand Down