Skip to content

Commit 592bc4d

Browse files
committed
FIX: add support for imshow extent to have units
1 parent 6eba0af commit 592bc4d

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
The *extent* parameter of imshow and set_extent functions can now be expressed with Units
2+
-----------------------------------------------------------------------------------------
3+
The extent parameter of `~axes.Axes.imshow` can now be expressed
4+
with Units. It internally uses the `~AxesImage.set_extent` which contains
5+
the implemenation that enables extent being expressed with Units.
6+
If extent[0] is expressed in units, then so must extent[1].
7+
Similarly, this applies to extent[2] and extent[3] as well. The user is
8+
responsible to ensure that the units are lined up.
9+
10+
.. plot::
11+
:include-source: true
12+
13+
import matplotlib.pyplot as plt
14+
import numpy as np
15+
from matplotlib.dates import HourLocator, DateFormatter
16+
from matplotlib.ticker import (AutoMinorLocator)
17+
18+
19+
fig, ax = plt.subplots()
20+
dates = np.arange("2020-01-01","2020-01-13", dtype='datetime64[h]')
21+
22+
arr = [[i+j for i in range(10)] for j in range(10)]
23+
24+
ax.imshow(arr, origin='lower', extent=[dates[0], dates[10], dates[10], dates[0]])
25+
26+
ax.xaxis.set_major_formatter(DateFormatter('%d/%m:%H'))
27+
ax.xaxis.set_major_locator(HourLocator(byhour=[0, 2, 4, 6, 8, 10]))
28+
ax.xaxis.set_minor_locator(AutoMinorLocator())
29+
30+
plt.show()

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5429,7 +5429,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54295429
See the :doc:`/tutorials/intermediate/imshow_extent` tutorial for
54305430
examples and a more detailed description.
54315431
5432-
extent : floats (left, right, bottom, top), optional
5432+
extent : floats or units (left, right, bottom, top), optional
54335433
The bounding box in data coordinates that the image will fill.
54345434
The image is stretched individually along x and y to fill the box.
54355435
@@ -5506,6 +5506,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55065506
if aspect is None:
55075507
aspect = rcParams['image.aspect']
55085508
self.set_aspect(aspect)
5509+
55095510
im = mimage.AxesImage(self, cmap, norm, interpolation,
55105511
origin, extent, filternorm=filternorm,
55115512
filterrad=filterrad, resample=resample,
@@ -5522,7 +5523,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55225523

55235524
# update ax.dataLim, and, if autoscaling, set viewLim
55245525
# to tightly fit the image, regardless of dataLim.
5525-
im.set_extent(im.get_extent())
5526+
im.set_extent(im.get_extent(), **kwargs)
55265527

55275528
self.add_image(im)
55285529
return im

lib/matplotlib/image.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def _check_unsampled_image(self):
949949
"""Return whether the image would be better drawn unsampled."""
950950
return self.get_interpolation() == "none"
951951

952-
def set_extent(self, extent):
952+
def set_extent(self, extent, **kwargs):
953953
"""
954954
Set the image extent.
955955
@@ -958,6 +958,10 @@ def set_extent(self, extent):
958958
extent : 4-tuple of float
959959
The position and size of the image as tuple
960960
``(left, right, bottom, top)`` in data coordinates.
961+
kwargs : dict
962+
Other parameters from which unit info (i.e., the *xunits*,
963+
*yunits*, *zunits* (for 3D axes), *runits* and *thetaunits* (for
964+
polar axes) entries) is popped, if present.
961965
962966
Notes
963967
-----
@@ -966,7 +970,21 @@ def set_extent(self, extent):
966970
state is not changed, so following this with ``ax.autoscale_view()``
967971
will redo the autoscaling in accord with ``dataLim``.
968972
"""
969-
self._extent = xmin, xmax, ymin, ymax = extent
973+
(xmin, xmax), = self.axes._process_unit_info(
974+
[("x", [extent[0], extent[1]])], kwargs)
975+
(ymin, ymax), = self.axes._process_unit_info(
976+
[("y", [extent[2], extent[3]])], kwargs)
977+
xmin = self.axes._validate_converted_limits(
978+
xmin, self.convert_xunits)
979+
xmax = self.axes._validate_converted_limits(
980+
xmax, self.convert_xunits)
981+
ymin = self.axes._validate_converted_limits(
982+
ymin, self.convert_yunits)
983+
ymax = self.axes._validate_converted_limits(
984+
ymax, self.convert_yunits)
985+
extent = [xmin, xmax, ymin, ymax]
986+
987+
self._extent = extent
970988
corners = (xmin, ymin), (xmax, ymax)
971989
self.axes.update_datalim(corners)
972990
self.sticky_edges.x[:] = [xmin, xmax]
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,3 +7539,13 @@ def test_clim():
75397539
clim = (7, 8)
75407540
norm = plot_method(clim=clim).norm
75417541
assert (norm.vmin, norm.vmax) == clim
7542+
7543+
7544+
@image_comparison(["extent_units.pdf"])
7545+
def test_extent_units():
7546+
mpl.style.use("mpl20")
7547+
fig, ax = plt.subplots()
7548+
dates = np.arange("2020-01-01", "2020-01-13", dtype='datetime64')
7549+
arr = [[i+j for i in range(10)] for j in range(10)]
7550+
ax.imshow(arr, origin='lower', extent=[0, 10,
7551+
dates[10], dates[0]])

0 commit comments

Comments
 (0)