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

Skip to content

Commit c54e2ba

Browse files
committed
Fix dpi detection in tight_bbox (fixes #2586).
1 parent 8c16c61 commit c54e2ba

File tree

7 files changed

+28
-69
lines changed

7 files changed

+28
-69
lines changed

doc/api/api_changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ original location:
119119
* Removed the class `FigureManagerQTAgg` and deprecated `NavigationToolbar2QTAgg`
120120
which will be removed in 1.5.
121121

122+
* The function signatures of `tight_bbox.adjust_bbox` and
123+
`tight_bbox.process_figure_for_rasterizing` have been changed. A new
124+
`fixed_dpi` parameter allows for overriding the `figure.dpi` setting
125+
instead of trying to deduce the intended behaviour from the file format.
126+
127+
122128
.. _changes_in_1_3:
123129

124130

lib/matplotlib/backend_bases.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,7 @@ class FigureCanvasBase(object):
16251625
]
16261626

16271627
supports_blit = True
1628+
fixed_dpi = None
16281629

16291630
filetypes = _default_filetypes
16301631
if _has_pil:
@@ -2171,8 +2172,8 @@ def print_figure(self, filename, dpi=None, facecolor='w', edgecolor='w',
21712172

21722173
bbox_inches = bbox_inches.padded(pad)
21732174

2174-
restore_bbox = tight_bbox.adjust_bbox(self.figure, format,
2175-
bbox_inches)
2175+
restore_bbox = tight_bbox.adjust_bbox(self.figure, bbox_inches,
2176+
canvas.fixed_dpi)
21762177

21772178
_bbox_inches_restore = (bbox_inches, restore_bbox)
21782179
else:

lib/matplotlib/backends/backend_mixed.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ def start_rasterizing(self):
9393

9494
if self._bbox_inches_restore: # when tight bbox is used
9595
r = process_figure_for_rasterizing(self.figure,
96-
self._bbox_inches_restore,
97-
mode="png")
98-
96+
self._bbox_inches_restore)
9997
self._bbox_inches_restore = r
10098

101-
10299
if self._rasterizing == 0:
103100
self._raster_renderer = self._raster_renderer_class(
104101
self._width*self.dpi, self._height*self.dpi, self.dpi)
@@ -143,5 +140,5 @@ def stop_rasterizing(self):
143140
if self._bbox_inches_restore: # when tight bbox is used
144141
r = process_figure_for_rasterizing(self.figure,
145142
self._bbox_inches_restore,
146-
mode="pdf")
143+
self._figdpi)
147144
self._bbox_inches_restore = r

lib/matplotlib/backends/backend_pdf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,8 @@ class FigureCanvasPdf(FigureCanvasBase):
24342434
figure - A Figure instance
24352435
"""
24362436

2437+
fixed_dpi = 72
2438+
24372439
def draw(self):
24382440
pass
24392441

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,8 @@ def new_figure_manager_given_figure(num, figure):
966966
class FigureCanvasPS(FigureCanvasBase):
967967
_renderer_class = RendererPS
968968

969+
fixed_dpi = 72
970+
969971
def draw(self):
970972
pass
971973

lib/matplotlib/backends/backend_svg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,8 @@ class FigureCanvasSVG(FigureCanvasBase):
11501150
filetypes = {'svg': 'Scalable Vector Graphics',
11511151
'svgz': 'Scalable Vector Graphics'}
11521152

1153+
fixed_dpi = 72
1154+
11531155
def print_svg(self, filename, *args, **kwargs):
11541156
if is_string_like(filename):
11551157
fh_to_close = svgwriter = io.open(filename, 'w', encoding='utf-8')

lib/matplotlib/tight_bbox.py

Lines changed: 11 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from matplotlib.transforms import Bbox, TransformedBbox, Affine2D
1212

1313

14-
def adjust_bbox(fig, format, bbox_inches):
14+
def adjust_bbox(fig, bbox_inches, fixed_dpi=None):
1515
"""
1616
Temporarily adjust the figure so that only the specified area
1717
(bbox_inches) is saved.
@@ -50,74 +50,31 @@ def restore_bbox():
5050
fig.transFigure.invalidate()
5151
fig.patch.set_bounds(0, 0, 1, 1)
5252

53-
adjust_bbox_handler = _adjust_bbox_handler_d.get(format)
54-
if adjust_bbox_handler is not None:
55-
adjust_bbox_handler(fig, bbox_inches)
56-
return restore_bbox
53+
if fixed_dpi is not None:
54+
tr = Affine2D().scale(fixed_dpi)
55+
dpi_scale = fixed_dpi / fig.dpi
5756
else:
58-
warnings.warn("bbox_inches option for %s backend is not "
59-
"implemented yet." % (format))
60-
return None
61-
62-
63-
def adjust_bbox_png(fig, bbox_inches):
64-
"""
65-
adjust_bbox for png (Agg) format
66-
"""
67-
68-
tr = fig.dpi_scale_trans
69-
70-
_bbox = TransformedBbox(bbox_inches,
71-
tr)
72-
x0, y0 = _bbox.x0, _bbox.y0
73-
fig.bbox_inches = Bbox.from_bounds(0, 0,
74-
bbox_inches.width,
75-
bbox_inches.height)
76-
77-
x0, y0 = _bbox.x0, _bbox.y0
78-
w1, h1 = fig.bbox.width, fig.bbox.height
79-
fig.transFigure._boxout = Bbox.from_bounds(-x0, -y0,
80-
w1, h1)
81-
fig.transFigure.invalidate()
82-
83-
fig.bbox = TransformedBbox(fig.bbox_inches, tr)
84-
85-
fig.patch.set_bounds(x0 / w1, y0 / h1,
86-
fig.bbox.width / w1, fig.bbox.height / h1)
87-
88-
89-
def adjust_bbox_pdf(fig, bbox_inches):
90-
"""
91-
adjust_bbox for pdf & eps format
92-
"""
93-
94-
if fig._cachedRenderer.__class__.__name__ == "RendererPgf":
9557
tr = Affine2D().scale(fig.dpi)
96-
f = 1.
97-
else:
98-
tr = Affine2D().scale(72)
99-
f = 72. / fig.dpi
58+
dpi_scale = 1.
10059

10160
_bbox = TransformedBbox(bbox_inches, tr)
10261

10362
fig.bbox_inches = Bbox.from_bounds(0, 0,
104-
bbox_inches.width,
105-
bbox_inches.height)
63+
bbox_inches.width, bbox_inches.height)
10664
x0, y0 = _bbox.x0, _bbox.y0
107-
w1, h1 = fig.bbox.width * f, fig.bbox.height * f
108-
fig.transFigure._boxout = Bbox.from_bounds(-x0, -y0,
109-
w1, h1)
65+
w1, h1 = fig.bbox.width * dpi_scale, fig.bbox.height * dpi_scale
66+
fig.transFigure._boxout = Bbox.from_bounds(-x0, -y0, w1, h1)
11067
fig.transFigure.invalidate()
11168

11269
fig.bbox = TransformedBbox(fig.bbox_inches, tr)
11370

11471
fig.patch.set_bounds(x0 / w1, y0 / h1,
11572
fig.bbox.width / w1, fig.bbox.height / h1)
11673

74+
return restore_bbox
11775

118-
def process_figure_for_rasterizing(figure,
119-
bbox_inches_restore, mode):
12076

77+
def process_figure_for_rasterizing(fig, bbox_inches_restore, fixed_dpi=None):
12178
"""
12279
This need to be called when figure dpi changes during the drawing
12380
(e.g., rasterizing). It recovers the bbox and re-adjust it with
@@ -126,14 +83,6 @@ def process_figure_for_rasterizing(figure,
12683

12784
bbox_inches, restore_bbox = bbox_inches_restore
12885
restore_bbox()
129-
r = adjust_bbox(figure, mode,
130-
bbox_inches)
86+
r = adjust_bbox(figure, bbox_inches, fixed_dpi)
13187

13288
return bbox_inches, r
133-
134-
135-
_adjust_bbox_handler_d = {}
136-
for format in ["png", "raw", "rgba", "jpg", "jpeg", "tiff"]:
137-
_adjust_bbox_handler_d[format] = adjust_bbox_png
138-
for format in ["pgf", "pdf", "eps", "svg", "svgz"]:
139-
_adjust_bbox_handler_d[format] = adjust_bbox_pdf

0 commit comments

Comments
 (0)