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

Skip to content

Commit 5235f52

Browse files
authored
Merge pull request #12117 from dstansby/extents
Fix Agg extent calculations for empty draws
2 parents b99e636 + b8cb5d8 commit 5235f52

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/matplotlib/tests/test_backend_pdf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,11 @@ def test_failing_latex(tmpdir):
232232
plt.xlabel("$22_2_2$")
233233
with pytest.raises(RuntimeError):
234234
plt.savefig(path)
235+
236+
237+
def test_empty_rasterised():
238+
# Check that emtpy figures that are rasterised save to pdf files fine
239+
with PdfPages(io.BytesIO()) as pdf:
240+
fig, ax = plt.subplots()
241+
ax.plot([], [], rasterized=True)
242+
fig.savefig(pdf, format="pdf")

src/_backend_agg.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,15 @@ agg::rect_i RendererAgg::get_content_extents()
201201
}
202202
}
203203

204-
r.x1 = std::max(0, r.x1);
205-
r.y1 = std::max(0, r.y1);
206-
r.x2 = std::min(r.x2 + 1, (int)width);
207-
r.y2 = std::min(r.y2 + 1, (int)height);
204+
if (r.x1 == width && r.x2 == 0) {
205+
// The buffer is completely empty.
206+
r.x1 = r.y1 = r.x2 = r.y2 = 0;
207+
} else {
208+
r.x1 = std::max(0, r.x1);
209+
r.y1 = std::max(0, r.y1);
210+
r.x2 = std::min(r.x2 + 1, (int)width);
211+
r.y2 = std::min(r.y2 + 1, (int)height);
212+
}
208213

209214
return r;
210215
}

0 commit comments

Comments
 (0)