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

Skip to content

Commit 953ee41

Browse files
committed
FIX: add support for imshow extent to have units
1 parent 9bcdd60 commit 953ee41

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
@@ -5531,6 +5531,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
55315531
55325532
extent : floats (left, right, bottom, top), optional
55335533
The bounding box in data coordinates that the image will fill.
5534+
These values may be unitful and match the units of the Axes.
55345535
The image is stretched individually along x and y to fill the box.
55355536
55365537
The default extent is determined by the following conditions.
@@ -5623,7 +5624,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
56235624

56245625
# update ax.dataLim, and, if autoscaling, set viewLim
56255626
# to tightly fit the image, regardless of dataLim.
5626-
im.set_extent(im.get_extent())
5627+
im.set_extent(im.get_extent(), **kwargs)
56275628

56285629
self.add_image(im)
56295630
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
@@ -8260,3 +8260,15 @@ def test_bar_all_nan(fig_test, fig_ref):
82608260

82618261
ax_ref.bar([1], [1]).remove()
82628262
ax_ref.bar([1], [1])
8263+
8264+
8265+
@image_comparison(["extent_units.pdf"], style="mpl20")
8266+
def test_extent_units():
8267+
mpl.style.use("mpl20")
8268+
_, ax = plt.subplots()
8269+
dateFirst = np.datetime64('2020-01-01', 'D')
8270+
dateLast = np.datetime64('2020-01-11', 'D')
8271+
8272+
arr = [[i+j for i in range(10)] for j in range(10)]
8273+
ax.imshow(arr, origin='lower',
8274+
extent=[1, 11, dateFirst, dateLast])

0 commit comments

Comments
 (0)
0