8000 FIX: add support for imshow extent to have units · matplotlib/matplotlib@462ad76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 462ad76

Browse files
committed
FIX: add support for imshow extent to have units
1 parent 924e210 commit 462ad76

File tree

5 files changed

+60
-3
lines changed

5 files changed

+60
-3
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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` and `~.AxesImage.set_extent`
4+
can now be expressed with units.
5+
6+
.. plot::
7+
:include-source: true
8+
9+
import matplotlib.pyplot as plt
10+
import numpy as np
11+
from matplotlib.dates import HourLocator, DateFormatter
12+
from matplotlib.ticker import AutoMinorLocator
13+
14+
15+
fig, ax = plt.subplots()
16+
dates = np.arange("2020-01-01","2020-01-13", dtype='datetime64[h]')
17+
18+
arr = [[i+j for i in range(10)] for j in range(10)]
19+
20+
ax.imshow(arr, origin='lower', extent=[dates[0], dates[10], dates[10], dates[0]])
21+
22+
ax.xaxis.set_major_formatter(DateFormatter('%d/%m:%H'))
23+
ax.xaxis.set_major_locator(HourLocator(byhour=[0, 2, 4, 6, 8, 10]))
24+
ax.xaxis.set_minor_locator(AutoMinorLocator())
25+
26+
plt.show()

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5523,6 +5523,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55235523
55245524
extent : floats (left, right, bottom, top), optional
55255525
The bounding box in data coordinates that the image will fill.
5526+
These values may be unitful and match the units of the Axes.
55265527
The image is stretched individually along x and y to fill the box.
55275528
55285529
The default extent is determined by the following conditions.
@@ -5615,7 +5616,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56155616

56165617
# update ax.dataLim, and, if autoscaling, set viewLim
56175618
# to tightly fit the image, regardless of dataLim.
5618-
im.set_extent(im.get_extent())
5619+
im.set_extent(im.get_extent(), **kwargs)
56195620

56205621
self.add_image(im)
56215622
return im

lib/matplotlib/image.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ def _check_unsampled_image(self):
953953
"""Return whether the image would be better drawn unsampled."""
954954
return self.get_interpolation() == "none"
955955

956-
def set_extent(self, extent):
956+
def set_extent(self, extent, **kwargs):
957957
"""
958958
Set the image extent.
959959
@@ -962,6 +962,10 @@ def set_extent(self, extent):
962962
extent : 4-tuple of float
963963
The position and size of the image as tuple
964964
``(left, right, bottom, top)`` in data coordinates.
965+
**kwargs
966+
Other parameters from which unit info (i.e., the *xunits*,
967+
*yunits*, *zunits* (for 3D axes), *runits* and *thetaunits* (for
968+
polar axes) entries are applied, if present.
965969
966970
Notes
967971
-----
@@ -970,7 +974,24 @@ def set_extent(self, extent):
970974
state is not changed, so following this with ``ax.autoscale_view()``
971975
will redo the autoscaling in accord with ``dataLim``.
972976
"""
973-
self._extent = xmin, xmax, ymin, ymax = extent
977+
(xmin, xmax), (ymin, ymax) = self.axes._process_unit_info(
978+
[("x", [extent[0], extent[1]]),
979+
("y", [extent[2], extent[3]])],
980+
kwargs)
981+
if len(kwargs):
982+
raise ValueError(f"""set_extent did not consume all of the kwargs
983+
passed {list(kwargs)!r} were unused""")
984+
xmin = self.axes._validate_converted_limits(
985+
xmin, self.convert_xunits)
986+
xmax = self.axes._validate_converted_limits(
987+
xmax, self.convert_xunits)
988+
ymin = self.axes._validate_converted_limits(
989+
ymin, self.convert_yunits)
990+
ymax = self.axes._validate_converted_limits(
991+
ymax, self.convert_yunits)
992+
extent = [xmin, xmax, ymin, ymax]
993+
994+
self._extent = extent
974995
corners = (xmin, ymin), (xmax, ymax)
975996
self.axes.update_datalim(corners)
976997
self.sticky_edges.x[:] = [xmin, xmax]
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,3 +8195,12 @@ def test_bar_leading_nan():
81958195
for b in rest:
81968196
assert np.isfinite(b.xy).all()
81978197
assert np.isfinite(b.get_width())
8198+
8199+
@image_comparison(["extent_units.pdf"])
8200+
def test_extent_units():
8201+
mpl.style.use("mpl20")
8202+
fig, ax = plt.subplots()
8203+
dates = np.arange("2020-01-01", "2020-01-13", dtype='datetime64')
8204+
arr = [[i+j for i in range(10)] for j in range(10)]
8205+
ax.imshow(arr, origin='lower',
8206+
extent=[0, 10, dates[10], dates[0]])

0 commit comments

Comments
 (0)
0