Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 052b52b

Browse files
committed
Merge pull request #9204 from jklymak/fiximage
FIX: Get proper renderer width and height in FigureImage
1 parent cece2d4 commit 052b52b

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

lib/matplotlib/image.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,11 +1117,20 @@ def get_extent(self):
11171117
-0.5 + self.oy, numrows-0.5 + self.oy)
11181118

11191119
def make_image(self, renderer, magnification=1.0, unsampled=False):
1120-
bbox = Bbox([[self.ox, self.oy],
1121-
[self.ox + self._A.shape[1], self.oy + self._A.shape[0]]])
1122-
clip = Bbox([[0, 0], [renderer.width, renderer.height]])
1120+
fac = renderer.dpi/self.figure.dpi
1121+
# fac here is to account for pdf, eps, svg backends where
1122+
# figure.dpi is set to 72. This means we need to scale the
1123+
# image (using magification) and offset it appropriately.
1124+
bbox = Bbox([[self.ox/fac, self.oy/fac],
1125+
[(self.ox/fac + self._A.shape[1]),
1126+
(self.oy/fac + self._A.shape[0])]])
1127+
width, height = self.figure.get_size_inches()
1128+
width *= renderer.dpi
1129+
height *= renderer.dpi
1130+
clip = Bbox([[0, 0], [width, height]])
1131+
11231132
return self._make_image(
1124-
self._A, bbox, bbox, clip, magnification=magnification,
1133+
self._A, bbox, bbox, clip, magnification=magnification / fac,
11251134
unsampled=unsampled, round_to_pixel_border=False)
11261135

11271136
def set_data(self, A):
Binary file not shown.
Binary file not shown.

lib/matplotlib/tests/test_image.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,36 @@ def test_interp_nearest_vs_none():
7272
ax2.set_title('interpolation nearest')
7373

7474

75-
@image_comparison(baseline_images=['figimage-0', 'figimage-1'], extensions=['png'])
76-
def test_figimage():
75+
def do_figimage(suppressComposite):
76+
""" Helper for the next two tests """
77+
fig = plt.figure(figsize=(2,2), dpi=100)
78+
fig.suppressComposite = suppressComposite
79+
x,y = np.ix_(np.arange(100.0)/100.0, np.arange(100.0)/100.0)
80+
z = np.sin(x**2 + y**2 - x*y)
81+
c = np.sin(20*x**2 + 50*y**2)
82+
img = z + c/5
83+
84+
fig.figimage(img, xo=0, yo=0, origin='lower')
85+
fig.figimage(img[::-1,:], xo=0, yo=100, origin='lower')
86+
fig.figimage(img[:,::-1], xo=100, yo=0, origin='lower')
87+
fig.figimage(img[::-1,::-1], xo=100, yo=100, origin='lower')
88+
89+
@image_comparison(baseline_images=['figimage-0'],
90+
extensions=['png','pdf'])
91+
def test_figimage0():
7792
'test the figimage method'
7893

79-
for suppressComposite in False, True:
80-
fig = plt.figure(figsize=(2,2), dpi=100)
81-
fig.suppressComposite = suppressComposite
82-
x,y = np.ix_(np.arange(100.0)/100.0, np.arange(100.0)/100.0)
83-
z = np.sin(x**2 + y**2 - x*y)
84-
c = np.sin(20*x**2 + 50*y**2)
85-
img = z + c/5
86-
87-
fig.figimage(img, xo=0, yo=0, origin='lower')
88-
fig.figimage(img[::-1,:], xo=0, yo=100, origin='lower')
89-
fig.figimage(img[:,::-1], xo=100, yo=0, origin='lower')
90-
fig.figimage(img[::-1,::-1], xo=100, yo=100, origin='lower')
94+
suppressComposite = False
95+
do_figimage(suppressComposite)
96+
97+
98+
@image_comparison(baseline_images=['figimage-1'],
99+
extensions=['png','pdf'])
100+
def test_figimage1():
101+
'test the figimage method'
102+
suppressComposite = True
103+
do_figimage(suppressComposite)
104+
91105

92106
def test_image_python_io():
93107
fig = plt.figure()

0 commit comments

Comments
 (0)