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

Skip to content

Commit db70db2

Browse files
committed
FIX: add support for imshow extent to have units
1 parent 68c78c9 commit db70db2

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
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+
date_first = np.datetime64('2020-01-01', 'D')
16+
date_last = 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, date_first, date_last])
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5555,6 +5555,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55555555
55565556
extent : floats (left, right, bottom, top), optional
55575557
The bounding box in data coordinates that the image will fill.
5558+
These values may be unitful and match the units of the Axes.
55585559
The image is stretched individually along x and y to fill the box.
55595560
55605561
The default extent is determined by the following conditions.

lib/matplotlib/image.py

Lines changed: 25 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,26 @@ 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(
983+
"set_extent did not consume all of the kwargs passed." +
984+
f"{list(kwargs)!r} were unused"
985+
)
986+
xmin = self.axes._validate_converted_limits(
987+
xmin, self.convert_xunits)
988+
xmax = self.axes._validate_converted_limits(
989+
xmax, self.convert_xunits)
990+
ymin = self.axes._validate_converted_limits(
991+
ymin, self.convert_yunits)
992+
ymax = self.axes._validate_converted_limits(
993+
ymax, self.convert_yunits)
994+
extent = [xmin, xmax, ymin, ymax]
995+
996+
self._extent = extent
974997
corners = (xmin, ymin), (xmax, ymax)
975998
self.axes.update_datalim(corners)
976999
self.sticky_edges.x[:] = [xmin, xmax]
Binary file not shown.
Binary file not shown.
Binary file not shown.

lib/matplotlib/tests/test_axes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8272,3 +8272,27 @@ def test_bar_all_nan(fig_test, fig_ref):
82728272

82738273
ax_ref.bar([1], [1]).remove()
82748274
ax_ref.bar([1], [1])
8275+
8276+
8277+
@image_comparison(["extent_units_y_axis.pdf"], style="mpl20")
8278+
def test_extent_units_y_axis():
8279+
mpl.style.use("mpl20")
8280+
_, ax = plt.subplots()
8281+
date_first = np.datetime64('2020-01-01', 'D')
8282+
date_last = np.datetime64('2020-01-11', 'D')
8283+
arr = [[i+j for i in range(10)] for j in range(10)]
8284+
im = ax.imshow(arr, origin='lower', extent=[1, 11, date_first, date_last])
8285+
with pytest.raises(ValueError,
8286+
match="set_extent did not consume all of the kwargs"):
8287+
im.set_extent([2, 12, date_first, date_last], clip=False)
8288+
8289+
8290+
@image_comparison(["extent_units_x_axis.pdf"], style="mpl20")
8291+
def test_extent_units_x_axis():
8292+
mpl.style.use("mpl20")
8293+
_, ax = plt.subplots()
8294+
date_first = np.datetime64('2020-01-11', 'D')
8295+
date_last = np.datetime64('2020-01-21', 'D')
8296+
arr = [[i+j for i in range(10)] for j in range(10)]
8297+
ax.xaxis.set_major_formatter(mdates.DateFormatter('Day%d'))
8298+
ax.imshow(arr, origin='lower', extent=[date_first, date_last, 1, 11])

0 commit comments

Comments
 (0)
0