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

Skip to content

Commit bde0f72

Browse files
committed
FIX: add support for imshow extent to have units
1 parent c0c3627 commit bde0f72

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5374,7 +5374,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
53745374
See the :doc:`/tutorials/intermediate/imshow_extent` tutorial for
53755375
examples and a more detailed description.
53765376
5377-
extent : floats (left, right, bottom, top), optional
5377+
extent : floats or units (left, right, bottom, top), optional
53785378
The bounding box in data coordinates that the image will fill.
53795379
The image is stretched individually along x and y to fill the box.
53805380
@@ -5451,6 +5451,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54515451
if aspect is None:
54525452
aspect = rcParams['image.aspect']
54535453
self.set_aspect(aspect)
5454+
54545455
im = mimage.AxesImage(self, cmap, norm, interpolation,
54555456
origin, extent, filternorm=filternorm,
54565457
filterrad=filterrad, resample=resample,
@@ -5467,7 +5468,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54675468

54685469
# update ax.dataLim, and, if autoscaling, set viewLim
54695470
# to tightly fit the image, regardless of dataLim.
5470-
im.set_extent(im.get_extent())
5471+
im.set_extent(im.get_extent(), **kwargs)
54715472

54725473
self.add_image(im)
54735474
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
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 are applied, 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), (ymin, ymax) = self.axes._process_unit_info(
974+
[("x", [extent[0], extent[1]]),
975+
("y", [extent[2], extent[3]])],
976+
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
@@ -7573,3 +7573,13 @@ def test_bezier_autoscale():
75737573
# Bottom ylim should be at the edge of the curve (-0.5), and not include
75747574
# the control point (at -1)
75757575
assert ax.get_ylim()[0] == -0.5
7576+
7577+
7578+
@image_comparison(["extent_units.pdf"])
7579+
def test_extent_units():
7580+
mpl.style.use("mpl20")
7581+
fig, ax = plt.subplots()
7582+
dates = np.arange("2020-01-01", "2020-01-13", dtype='datetime64')
7583+
arr = [[i+j for i in range(10)] for j in range(10)]
7584+
ax.imshow(arr, origin='lower',
7585+
extent=[0, 10, dates[10], dates[0]])

0 commit comments

Comments
 (0)
0