Skip to content

Commit 98506d0

Browse files
authored
Merge pull request #27964 from AnsonTran/fix-logscale-axis
BUG: Fix NonUniformImage with nonlinear scale
2 parents a218d5c + 919a6cb commit 98506d0

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

lib/matplotlib/image.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1085,12 +1085,16 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
10851085
B[:, :, 0:3] = A
10861086
B[:, :, 3] = 255
10871087
A = B
1088-
vl = self.axes.viewLim
10891088
l, b, r, t = self.axes.bbox.extents
10901089
width = int(((round(r) + 0.5) - (round(l) - 0.5)) * magnification)
10911090
height = int(((round(t) + 0.5) - (round(b) - 0.5)) * magnification)
1092-
x_pix = np.linspace(vl.x0, vl.x1, width)
1093-
y_pix = np.linspace(vl.y0, vl.y1, height)
1091+
1092+
invertedTransform = self.axes.transData.inverted()
1093+
x_pix = invertedTransform.transform(
1094+
[(x, b) for x in np.linspace(l, r, width)])[:, 0]
1095+
y_pix = invertedTransform.transform(
1096+
[(l, y) for y in np.linspace(b, t, height)])[:, 1]
1097+
10941098
if self._interpolation == "nearest":
10951099
x_mid = (self._Ax[:-1] + self._Ax[1:]) / 2
10961100
y_mid = (self._Ay[:-1] + self._Ay[1:]) / 2

lib/matplotlib/tests/test_image.py

+21
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,27 @@ def test_nonuniform_and_pcolor():
14141414
ax.set(xlim=(0, 10))
14151415

14161416

1417+
@image_comparison(["nonuniform_logscale.png"], style="mpl20")
1418+
def test_nonuniform_logscale():
1419+
_, axs = plt.subplots(ncols=3, nrows=1)
1420+
1421+
for i in range(3):
1422+
ax = axs[i]
1423+
im = NonUniformImage(ax)
1424+
im.set_data(np.arange(1, 4) ** 2, np.arange(1, 4) ** 2,
1425+
np.arange(9).reshape((3, 3)))
1426+
ax.set_xlim(1, 16)
1427+
ax.set_ylim(1, 16)
1428+
ax.set_box_aspect(1)
1429+
if i == 1:
1430+
ax.set_xscale("log", base=2)
1431+
ax.set_yscale("log", base=2)
1432+
if i == 2:
1433+
ax.set_xscale("log", base=4)
1434+
ax.set_yscale("log", base=4)
1435+
ax.add_image(im)
1436+
1437+
14171438
@image_comparison(
14181439
['rgba_antialias.png'], style='mpl20', remove_text=True,
14191440
tol=0 if platform.machine() == 'x86_64' else 0.007)

0 commit comments

Comments
 (0)