8000 Merge pull request #5815 from mdboom/fix-minimizing-raster-layer · matplotlib/matplotlib@90455d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 90455d8

Browse files
committed
Merge pull request #5815 from mdboom/fix-minimizing-raster-layer
Properly minimize the rasterized layers
2 parents 5eec831 + 2c99e51 commit 90455d8

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

lib/matplotlib/tests/test_image.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,40 @@ def test_jpeg_alpha():
481481
assert image.getpixel((0, 0)) == (254, 0, 0)
482482

483483

484+
@cleanup
485+
def test_minimized_rasterized():
486+
# This ensures that the rasterized content in the colorbars is
487+
# only as thick as the colorbar, and doesn't extend to other parts
488+
# of the image. See #5814. While the original bug exists only
489+
# in Postscript, the best way to detect it is to generate SVG
490+
# and then parse the output to make sure the two colorbar images
491+
# are the same size.
492+
from xml.etree import ElementTree
493+
494+
np.random.seed(0)
495+
data = np.random.rand(10, 10)
496+
497+
fig, ax = plt.subplots(1, 2)
498+
p1 = ax[0].pcolormesh(data)
499+
p2 = ax[1].pcolormesh(data)
500+
501+
plt.colorbar(p1, ax=ax[0])
502+
plt.colorbar(p2, ax=ax[1])
503+
504+
buff = io.BytesIO()
505+
plt.savefig(buff, format='svg')
506+
507+
buff = io.BytesIO(buff.getvalue())
508+
tree = ElementTree.parse(buff)
509+
width = None
510+
for image in tree.iter('image'):
511+
if width is None:
512+
width = image['width']
513+
else:
514+
if image['width'] != width:
515+
assert False
516+
517+
484518
if __name__=='__main__':
485519
import nose
486520
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

src/_backend_agg.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ agg::rect_i RendererAgg::get_content_extents()
210210
}
211211
}
212212

213-
r.x1 = std::max(0, r.x1 - 1);
214-
r.y1 = std::max(0, r.y1 - 1);
215-
r.x2 = std::max(r.x2 + 1, (int)width);
216-
r.y2 = std::max(r.y2 + 1, (int)height);
213+
r.x1 = std::max(0, r.x1);
214+
r.y1 = std::max(0, r.y1);
215+
r.x2 = std::min(r.x2 + 1, (int)width);
216+
r.y2 = std::min(r.y2 + 1, (int)height);
217217

218218
return r;
219219
}

0 commit comments

Comments
 (0)
0