99import os
1010
1111import numpy as np
12-
1312from matplotlib import cm , rcParams
13+ from matplotlib .backends .backend_pdf import PdfPages
1414from matplotlib import pyplot as plt
1515from matplotlib .testing .decorators import (image_comparison , knownfailureif ,
1616 cleanup )
@@ -42,7 +42,6 @@ def test_type42():
4242
4343@cleanup
4444def test_multipage_pagecount ():
45- from matplotlib .backends .backend_pdf import PdfPages
4645 with PdfPages (io .BytesIO ()) as pdf :
4746 assert pdf .get_pagecount () == 0
4847 fig = plt .figure ()
@@ -58,7 +57,7 @@ def test_multipage_pagecount():
5857def test_multipage_keep_empty ():
5958 from matplotlib .backends .backend_pdf import PdfPages
6059 from tempfile import NamedTemporaryFile
61- ### test empty pdf files
60+ # test empty pdf files
6261 # test that an empty pdf is left behind with keep_empty=True (default)
6362 with NamedTemporaryFile (delete = False ) as tmp :
6463 with PdfPages (tmp ) as pdf :
@@ -69,7 +68,7 @@ def test_multipage_keep_empty():
6968 with PdfPages (filename , keep_empty = False ) as pdf :
7069 pass
7170 assert not os .path .exists (filename )
72- ### test pdf files with content, they should never be deleted
71+ # test pdf files with content, they should never be deleted
7372 fig = plt .figure ()
7473 ax = fig .add_subplot (111 )
7574 ax .plot ([1 , 2 , 3 ])
@@ -87,3 +86,24 @@ def test_multipage_keep_empty():
8786 pdf .savefig ()
8887 assert os .path .exists (filename )
8988 os .remove (filename )
89+
90+
91+ @cleanup
92+ def test_composite_image ():
93+ #Test that figures can be saved with and without combining multiple images
94+ #(on a single set of axes) into a single composite image.
95+ X , Y = np .meshgrid (np .arange (- 5 , 5 , 1 ), np .arange (- 5 , 5 , 1 ))
96+ Z = np .sin (Y ** 2 )
97+ fig = plt .figure ()
98+ ax = fig .add_subplot (1 , 1 , 1 )
99+ ax .set_xlim (0 , 3 )
100+ ax .imshow (Z , extent = [0 , 1 , 0 , 1 ])
101+ ax .imshow (Z [::- 1 ], extent = [2 , 3 , 0 , 1 ])
102+ plt .rcParams ['image.composite_image' ] = True
103+ with PdfPages (io .BytesIO ()) as pdf :
104+ fig .savefig (pdf , format = "pdf" )
105+ assert len (pdf ._file .images .keys ()) == 1
106+ plt .rcParams ['image.composite_image' ] = False
107+ with PdfPages (io .BytesIO ()) as pdf :
108+ fig .savefig (pdf , format = "pdf" )
109+ assert len (pdf ._file .images .keys ()) == 2
0 commit comments