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

Skip to content

Commit ceadcea

Browse files
committed
FIX: add support for imshow extent to have units
1 parent 719c6b7 commit ceadcea

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Imshow Extents can now have Units
2+
--------------------------------
3+
The extent parameter of `~axes.Axes.imshow` can now be expressed
4+
with Units. If extent[0] is expressed in units, then so must extent[1].
5+
Similarly, this applies to extent[2] and extent[3] as well. The user is
6+
responsible to ensure that the units are lined up.
7+
8+
.. plot::
9+
:include-source: true
10+
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
from matplotlib.dates import HourLocator, DateFormatter
14+
from matplotlib.ticker import (AutoMinorLocator)
15+
16+
17+
fig, ax = plt.subplots()
18+
dates = np.arange("2020-01-01","2020-01-13", dtype='datetime64[h]')
19+
20+
arr = [[i+j for i in range(10)] for j in range(10)]
21+
22+
ax.imshow(arr, origin='lower', extent=[dates[0], dates[10], dates[10], dates[0]])
23+
24+
ax.xaxis.set_major_formatter(DateFormatter('%d/%m:%H'))
25+
ax.xaxis.set_major_locator(HourLocator(byhour=[0, 2, 4, 6, 8, 10]))
26+
ax.xaxis.set_minor_locator(AutoMinorLocator())
27+
28+
plt.show()

lib/matplotlib/axes/_axes.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5409,7 +5409,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54095409
See the :doc:`/tutorials/intermediate/imshow_extent` tutorial for
54105410
examples and a more detailed description.
54115411
5412-
extent : floats (left, right, bottom, top), optional
5412+
extent : floats or units (left, right, bottom, top), optional
54135413
The bounding box in data coordinates that the image will fill.
54145414
The image is stretched individually along x and y to fill the box.
54155415
@@ -5486,6 +5486,24 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
54865486
if aspect is None:
54875487
aspect = rcParams['image.aspect']
54885488
self.set_aspect(aspect)
5489+
5490+
if extent is not None:
5491+
(left, right), = self._process_unit_info(
5492+
[("x", [extent[0], extent[1]])], kwargs)
5493+
(bottom, top), = self._process_unit_info(
5494+
[("y", [extent[2], extent[3]])], kwargs)
5495+
5496+
left = self._validate_converted_limits(
5497+
left, self.convert_xunits)
5498+
right = self._validate_converted_limits(
5499+
right, self.convert_xunits)
5500+
bottom = self._validate_converted_limits(
5501+
bottom, self.convert_yunits)
5502+
top = self._validate_converted_limits(
5503+
top, self.convert_yunits)
5504+
5505+
extent = [left, right, bottom, top]
5506+
54895507
im = mimage.AxesImage(self, cmap, norm, interpolation,
54905508
origin, extent, filternorm=filternorm,
54915509
filterrad=filterrad, resample=resample,

lib/matplotlib/tests/test_axes.py

< 8949 div class="d-flex mr-2 flex-justify-end flex-items-center flex-1">
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7387,3 +7387,12 @@ def test_clim():
73877387
clim = (7, 8)
73887388
norm = plot_method(clim=clim).norm
73897389
assert (norm.vmin, norm.vmax) == clim
7390+
7391+
7392+
@image_comparison(["axes_extent_units"])
7393+
def test_extent_units():
7394+
fig, ax = plt.subplots()
7395+
dates = np.arange("2020-01-01", "2020-01-13", dtype='datetime64[h]')
7396+
arr = [[i+j for i in range(10)] for j in range(10)]
7397+
ax.imshow(arr, origin='lower', extent=[dates[0], dates[10],
7398+
dates[10], dates[0]])

0 commit comments

Comments
 (0)
0