@@ -22,7 +22,7 @@ def add_image(
2222 filename ,
2323 * ,
2424 width = NoEscape (r"0.8\textwidth" ),
25- placement = NoEscape (r"\centering" )
25+ placement = NoEscape (r"\centering" ),
2626 ):
2727 """Add an image to the figure.
2828
@@ -50,7 +50,7 @@ def add_image(
5050 StandAloneGraphic (image_options = width , filename = fix_filename (filename ))
5151 )
5252
53- def _save_plot (self , * args , extension = "pdf" , ** kwargs ):
53+ def _save_plot (self , * args , figure = None , extension = "pdf" , ** kwargs ):
5454 """Save the plot.
5555
5656 Returns
@@ -64,11 +64,12 @@ def _save_plot(self, *args, extension="pdf", **kwargs):
6464 filename = "{}.{}" .format (str (uuid .uuid4 ()), extension .strip ("." ))
6565 filepath = posixpath .join (tmp_path , filename )
6666
67- plt .savefig (filepath , * args , ** kwargs )
67+ fig = figure or plt .gcf ()
68+ fig .savefig (filepath , * args , ** kwargs )
6869 return filepath
6970
70- def add_plot (self , * args , extension = "pdf" , ** kwargs ):
71- """Add the current Matplotlib plot to the figure.
71+ def add_plot (self , * args , figure = None , extension = "pdf" , ** kwargs ):
72+ """Add a Matplotlib plot to the figure.
7273
7374 The plot that gets added is the one that would normally be shown when
7475 using ``plt.show()``.
@@ -77,6 +78,8 @@ def add_plot(self, *args, extension="pdf", **kwargs):
7778 ----
7879 args:
7980 Arguments passed to plt.savefig for displaying the plot.
81+ figure:
82+ Optional matplotlib figure. If None add the current figure.
8083 extension : str
8184 extension of image file indicating figure file type
8285 kwargs:
@@ -92,7 +95,7 @@ def add_plot(self, *args, extension="pdf", **kwargs):
9295 if key in kwargs :
9396 add_image_kwargs [key ] = kwargs .pop (key )
9497
95- filename = self ._save_plot (* args , extension = extension , ** kwargs )
98+ filename = self ._save_plot (* args , figure = figure , extension = extension , ** kwargs )
9699
97100 self .add_image (filename , ** add_image_kwargs )
98101
0 commit comments