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

Skip to content

Commit 4aa8a58

Browse files
Michael Weltermdboom
authored andcommitted
fix: dpi setting should work now for rasterization in svg backend
1 parent c39d9dc commit 4aa8a58

2 files changed

Lines changed: 46 additions & 12 deletions

File tree

lib/matplotlib/backends/backend_svg.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ class RendererSVG(RendererBase):
249249
FONT_SCALE = 100.0
250250
fontd = maxdict(50)
251251

252-
def __init__(self, width, height, svgwriter, basename=None):
252+
def __init__(self, width, height, image_dpi, svgwriter, basename=None):
253253
self.width = width
254254
self.height = height
255255
self.writer = XMLWriter(svgwriter)
256+
self.image_dpi = image_dpi # the actual dpi we want to rasterize stuff with
256257

257258
self._groupd = {}
258259
if not rcParams['svg.image_inline']:
@@ -747,6 +748,11 @@ def draw_gouraud_triangles(self, gc, triangles_array, colors_array,
747748
def option_scale_image(self):
748749
return True
749750

751+
752+
def get_image_magnification(self):
753+
return self.image_dpi/72.0
754+
755+
750756
def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
751757
attrib = {}
752758
clipid = self._get_clip(gc)
@@ -769,6 +775,17 @@ def draw_image(self, gc, x, y, im, dx=None, dy=None, transform=None):
769775
im.resize(numcols, numrows)
770776

771777
h,w = im.get_size_out()
778+
779+
if dx is None:
780+
w = 72.0*w/self.image_dpi
781+
else:
782+
w = dx
783+
784+
if dy is None:
785+
h = 72.0*h/self.image_dpi
786+
else:
787+
h = dy
788+
772789
oid = getattr(im, '_gid', None)
773790
url = getattr(im, '_url', None)
774791
if url is not None:
@@ -1154,25 +1171,17 @@ def print_svgz(self, filename, *args, **kwargs):
11541171

11551172
def _print_svg(self, filename, svgwriter, fh_to_close=None, **kwargs):
11561173
try:
1174+
image_dpi = kwargs.pop("dpi", 72)
11571175
self.figure.set_dpi(72.0)
11581176
width, height = self.figure.get_size_inches()
11591177
w, h = width*72, height*72
11601178

11611179
if rcParams['svg.image_noscale']:
1162-
renderer = RendererSVG(w, h, svgwriter, filename)
1180+
renderer = RendererSVG(w, h, image_dpi, svgwriter, filename)
11631181
else:
1164-
# setting mixed renderer dpi other than 72 results in
1165-
# incorrect size of the rasterized image. It seems that the
1166-
# svg internally uses fixed dpi of 72 and seems to cause
1167-
# the problem. I hope someone who knows the svg backends
1168-
# take a look at this problem. Meanwhile, the dpi
1169-
# parameter is ignored and image_dpi is fixed at 72. - JJL
1170-
1171-
#image_dpi = kwargs.pop("dpi", 72)
1172-
image_dpi = 72
11731182
_bbox_inches_restore = kwargs.pop("bbox_inches_restore", None)
11741183
renderer = MixedModeRenderer(self.figure,
1175-
width, height, image_dpi, RendererSVG(w, h, svgwriter, filename),
1184+
width, height, image_dpi, RendererSVG(w, h, image_dpi, svgwriter, filename),
11761185
bbox_inches_restore=_bbox_inches_restore)
11771186

11781187
self.figure.draw(renderer)

lib/matplotlib/tests/test_image.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,31 @@ def test_image_composite_alpha():
264264
ax.set_ylim([5, 0])
265265

266266

267+
@image_comparison(baseline_images=['rasterize_10dpi'], extensions=['pdf','svg'], tol=1.5e-3, remove_text=True)
268+
def test_rasterize_dpi():
269+
# This test should check rasterized rendering with high output resolution.
270+
# It plots a rasterized line and a normal image with implot. So it will catch
271+
# when images end up in the wrong place in case of non-standard dpi setting.
272+
# Instead of high-res rasterization i use low-res. Therefor the fact that the
273+
# resolution is non-standard is is easily checked by image_comparison.
274+
import numpy as np
275+
import matplotlib.pyplot as plt
276+
277+
img = np.asarray([[1, 2], [3, 4]])
278+
279+
fig, axes = plt.subplots(1, 3, figsize = (3, 1))
280+
281+
axes[0].imshow(img)
282+
283+
axes[1].plot([0,1],[0,1], linewidth=20., rasterized=True)
284+
axes[1].set(xlim = (0,1), ylim = (-1, 2))
285+
286+
axes[2].plot([0,1],[0,1], linewidth=20.)
287+
axes[2].set(xlim = (0,1), ylim = (-1, 2))
288+
289+
rcParams['savefig.dpi'] = 10
290+
291+
267292
if __name__=='__main__':
268293
import nose
269294
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

0 commit comments

Comments
 (0)