Skip to content

Commit a3aad58

Browse files
authored
Merge pull request #29783 from dstansby/pcolormesh-log-lims
Fix log scaling for pcolor and pcolormesh
2 parents ade791f + e469baa commit a3aad58

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6231,30 +6231,8 @@ def pcolor(self, *args, shading=None, alpha=None, norm=None, cmap=None,
62316231
collection._check_exclusionary_keywords(colorizer, vmin=vmin, vmax=vmax)
62326232
collection._scale_norm(norm, vmin, vmax)
62336233

6234-
# Transform from native to data coordinates?
6235-
t = collection._transform
6236-
if (not isinstance(t, mtransforms.Transform) and
6237-
hasattr(t, '_as_mpl_transform')):
6238-
t = t._as_mpl_transform(self.axes)
6239-
6240-
if t and any(t.contains_branch_seperately(self.transData)):
6241-
trans_to_data = t - self.transData
6242-
pts = np.vstack([x, y]).T.astype(float)
6243-
transformed_pts = trans_to_data.transform(pts)
6244-
x = transformed_pts[..., 0]
6245-
y = transformed_pts[..., 1]
6246-
6247-
self.add_collection(collection, autolim=False)
6248-
6249-
minx = np.min(x)
6250-
maxx = np.max(x)
6251-
miny = np.min(y)
6252-
maxy = np.max(y)
6253-
collection.sticky_edges.x[:] = [minx, maxx]
6254-
collection.sticky_edges.y[:] = [miny, maxy]
6255-
corners = (minx, miny), (maxx, maxy)
6256-
self.update_datalim(corners)
6257-
self._request_autoscale_view()
6234+
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
6235+
self._update_pcolor_lims(collection, coords)
62586236
return collection
62596237

62606238
@_preprocess_data()
@@ -6464,7 +6442,13 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
64646442
collection._scale_norm(norm, vmin, vmax)
64656443

64666444
coords = coords.reshape(-1, 2) # flatten the grid structure; keep x, y
6445+
self._update_pcolor_lims(collection, coords)
6446+
return collection
64676447

6448+
def _update_pcolor_lims(self, collection, coords):
6449+
"""
6450+
Common code for updating lims in pcolor() and pcolormesh() methods.
6451+
"""
64686452
# Transform from native to data coordinates?
64696453
t = collection._transform
64706454
if (not isinstance(t, mtransforms.Transform) and
@@ -6481,10 +6465,8 @@ def pcolormesh(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
64816465
maxx, maxy = np.max(coords, axis=0)
64826466
collection.sticky_edges.x[:] = [minx, maxx]
64836467
collection.sticky_edges.y[:] = [miny, maxy]
6484-
corners = (minx, miny), (maxx, maxy)
6485-
self.update_datalim(corners)
6468+
self.update_datalim(coords)
64866469
self._request_autoscale_view()
6487-
return collection
64886470

64896471
@_preprocess_data()
64906472
@_docstring.interpd

lib/matplotlib/tests/test_axes.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,27 @@ def test_pcolormesh_nearest_noargs(fig_test, fig_ref):
14971497
ax.pcolormesh(x, y, C, shading="nearest")
14981498

14991499

1500+
@check_figures_equal(extensions=["png"])
1501+
def test_pcolormesh_log_scale(fig_test, fig_ref):
1502+
"""
1503+
Check that setting a log scale sets good default axis limits
1504+
when using pcolormesh.
1505+
"""
1506+
x = np.linspace(0, 1, 11)
1507+
y = np.linspace(1, 2, 5)
1508+
X, Y = np.meshgrid(x, y)
1509+
C = X + Y
1510+
1511+
ax = fig_test.subplots()
1512+
ax.pcolormesh(X, Y, C)
1513+
ax.set_xscale('log')
1514+
1515+
ax = fig_ref.subplots()
1516+
ax.pcolormesh(X, Y, C)
1517+
ax.set_xlim(1e-2, 1e1)
1518+
ax.set_xscale('log')
1519+
1520+
15001521
@image_comparison(['pcolormesh_datetime_axis.png'], style='mpl20')
15011522
def test_pcolormesh_datetime_axis():
15021523
# Remove this line when this test image is regenerated.
@@ -1550,6 +1571,27 @@ def test_pcolor_datetime_axis():
15501571
label.set_rotation(30)
15511572

15521573

1574+
@check_figures_equal(extensions=["png"])
1575+
def test_pcolor_log_scale(fig_test, fig_ref):
1576+
"""
1577+
Check that setting a log scale sets good default axis limits
1578+
when using pcolor.
1579+
"""
1580+
x = np.linspace(0, 1, 11)
1581+
y = np.linspace(1, 2, 5)
1582+
X, Y = np.meshgrid(x, y)
1583+
C = X[:-1, :-1] + Y[:-1, :-1]
1584+
1585+
ax = fig_test.subplots()
1586+
ax.pcolor(X, Y, C)
1587+
ax.set_xscale('log')
1588+
1589+
ax = fig_ref.subplots()
1590+
ax.pcolor(X, Y, C)
1591+
ax.set_xlim(1e-1, 1e0)
1592+
ax.set_xscale('log')
1593+
1594+
15531595
def test_pcolorargs():
15541596
n = 12
15551597
x = np.linspace(-1.5, 1.5, n)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy