@@ -44,7 +44,7 @@ def add_image(self, filename, *, width=NoEscape(r'0.8\textwidth'),
4444 self .append (StandAloneGraphic (image_options = width ,
4545 filename = fix_filename (filename )))
4646
47- def _save_plot (self , * args , extension = 'pdf' , ** kwargs ):
47+ def _save_plot (self , * args , figure = None , extension = 'pdf' , ** kwargs ):
4848 """Save the plot.
4949
5050 Returns
@@ -58,11 +58,12 @@ def _save_plot(self, *args, extension='pdf', **kwargs):
5858 filename = '{}.{}' .format (str (uuid .uuid4 ()), extension .strip ('.' ))
5959 filepath = posixpath .join (tmp_path , filename )
6060
61- plt .savefig (filepath , * args , ** kwargs )
61+ fig = figure or plt .gcf ()
62+ fig .savefig (filepath , * args , ** kwargs )
6263 return filepath
6364
64- def add_plot (self , * args , extension = 'pdf' , ** kwargs ):
65- """Add the current Matplotlib plot to the figure.
65+ def add_plot (self , * args , figure = None , extension = 'pdf' , ** kwargs ):
66+ """Add a Matplotlib plot to the figure.
6667
6768 The plot that gets added is the one that would normally be shown when
6869 using ``plt.show()``.
@@ -71,6 +72,8 @@ def add_plot(self, *args, extension='pdf', **kwargs):
7172 ----
7273 args:
7374 Arguments passed to plt.savefig for displaying the plot.
75+ figure:
76+ Optional matplotlib figure. If None add the current figure.
7477 extension : str
7578 extension of image file indicating figure file type
7679 kwargs:
@@ -86,7 +89,9 @@ def add_plot(self, *args, extension='pdf', **kwargs):
8689 if key in kwargs :
8790 add_image_kwargs [key ] = kwargs .pop (key )
8891
89- filename = self ._save_plot (* args , extension = extension , ** kwargs )
92+ filename = self ._save_plot (
93+ * args , figure = figure , extension = extension , ** kwargs
94+ )
9095
9196 self .add_image (filename , ** add_image_kwargs )
9297
0 commit comments