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

Skip to content

Commit b0e758d

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

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* of ``imshow`` 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+
fig, ax = plt.subplots()
15+
dateFirst = np.datetime64('2020-01-01', 'D')
16+
dateLast = np.datetime64('2020-01-11', 'D')
17+
18+
arr = [[i+j for i in range(10)] for j in range(10)]
19+
20+
ax.imshow(arr, origin='lower', extent=[1, 11, dateFirst, dateLast])
21+
22+
ax.yaxis.set_major_formatter(DateFormatter('%d/%m/%y:- %H00hours'))
23+
ax.yaxis.set_major_locator(HourLocator(byhour=[0, 6, 12, 18, 24]))
24+
ax.yaxis.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: 20 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,21 @@ 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+
xmin = self.axes._validate_converted_limits(
982+
xmin, self.convert_xunits)
983+
xmax = self.axes._validate_converted_limits(
984+
xmax, self.convert_xunits)
985+
ymin = self.axes._validate_converted_limits(
986+
ymin, self.convert_yunits)
987+
ymax = self.axes._validate_converted_limits(
988+
ymax, self.convert_yunits)
989+
extent = [xmin, xmax, ymin, ymax]
990+
991+
self._extent = extent
974992
corners = (xmin, ymin), (xmax, ymax)
975993
self.axes.update_datalim(corners)
976994
self.sticky_edges.x[:] = [xmin, xmax]
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8195,3 +8195,15 @@ 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+
8200+
@image_comparison(["extent_units.pdf"], style="mpl20")
8201+
def test_extent_units():
8202+
mpl.style.use("mpl20")
8203+
_, ax = plt.subplots()
8204+
dateFirst = np.datetime64('2020-01-01', 'D')
8205+
dateLast = np.datetime64('2020-01-11', 'D')
8206+
8207+
arr = [[i+j for i in range(10)] for j in range(10)]
8208+
ax.imshow(arr, origin='lower',
8209+
extent=[1, 11, dateFirst, dateLast])

0 commit comments

Comments
 (0)
0