Skip to content

Backport PR #12117 on branch v2.2.x (Fix Agg extent calculations for empty draws) #12125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,11 @@ def test_failing_latex(tmpdir):
plt.xlabel("$22_2_2$")
with pytest.raises(RuntimeError):
plt.savefig(path)


def test_empty_rasterised():
# Check that emtpy figures that are rasterised save to pdf files fine
with PdfPages(io.BytesIO()) as pdf:
fig, ax = plt.subplots()
ax.plot([], [], rasterized=True)
fig.savefig(pdf, format="pdf")
13 changes: 9 additions & 4 deletions src/_backend_agg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,15 @@ agg::rect_i RendererAgg::get_content_extents()
}
}

r.x1 = std::max(0, r.x1);
r.y1 = std::max(0, r.y1);
r.x2 = std::min(r.x2 + 1, (int)width);
r.y2 = std::min(r.y2 + 1, (int)height);
if (r.x1 == width && r.x2 == 0) {
// The buffer is completely empty.
r.x1 = r.y1 = r.x2 = r.y2 = 0;
} else {
r.x1 = std::max(0, r.x1);
r.y1 = std::max(0, r.y1);
r.x2 = std::min(r.x2 + 1, (int)width);
r.y2 = std::min(r.y2 + 1, (int)height);
}

return r;
}
Expand Down