Skip to content

Commit 7487c2f

Browse files
committed
Fix BoundingBox in EPS files.
The elements should be integers; for floating point, the `HiResBoundingBox` can be used.
1 parent ec45ec7 commit 7487c2f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ def print_figure_impl(fh):
910910
end="", file=fh)
911911
print(f"{dsc_comments}\n"
912912
f"%%Orientation: {orientation.name}\n"
913-
f"%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}\n"
913+
f"{get_bbox_header(bbox)[0]}\n"
914914
f"%%EndComments\n",
915915
end="", file=fh)
916916

@@ -1056,7 +1056,7 @@ def write(self, *args, **kwargs):
10561056
f"""\
10571057
%!PS-Adobe-3.0 EPSF-3.0
10581058
{dsc_comments}
1059-
%%BoundingBox: {bbox[0]} {bbox[1]} {bbox[2]} {bbox[3]}
1059+
{get_bbox_header(bbox)[0]}
10601060
%%EndComments
10611061
%%BeginProlog
10621062
/mpldict {len(psDefs)} dict def

lib/matplotlib/tests/test_backend_ps.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,24 @@ def test_transparency():
113113
ax.text(.5, .5, "foo", color="r", alpha=0)
114114

115115

116+
def test_bbox():
117+
fig, ax = plt.subplots()
118+
with io.BytesIO() as buf:
119+
fig.savefig(buf, format='eps')
120+
buf = buf.getvalue()
121+
122+
bb = re.search(b'^%%BoundingBox: (.+) (.+) (.+) (.+)$', buf, re.MULTILINE)
123+
assert bb
124+
hibb = re.search(b'^%%HiResBoundingBox: (.+) (.+) (.+) (.+)$', buf,
125+
re.MULTILINE)
126+
assert hibb
127+
128+
for i in range(1, 5):
129+
# BoundingBox must use integers, and be ceil/floor of the hi res.
130+
assert b'.' not in bb.group(i)
131+
assert int(bb.group(i)) == pytest.approx(float(hibb.group(i)), 1)
132+
133+
116134
@needs_usetex
117135
def test_failing_latex():
118136
"""Test failing latex subprocess call"""

0 commit comments

Comments
 (0)