@@ -44,7 +44,7 @@ def add_image(self, filename, *, width=NoEscape(r'0.8\textwidth'),
44
44
self .append (StandAloneGraphic (image_options = width ,
45
45
filename = fix_filename (filename )))
46
46
47
- def _save_plot (self , * args , extension = 'pdf' , ** kwargs ):
47
+ def _save_plot (self , * args , figure = None , extension = 'pdf' , ** kwargs ):
48
48
"""Save the plot.
49
49
50
50
Returns
@@ -58,11 +58,12 @@ def _save_plot(self, *args, extension='pdf', **kwargs):
58
58
filename = '{}.{}' .format (str (uuid .uuid4 ()), extension .strip ('.' ))
59
59
filepath = posixpath .join (tmp_path , filename )
60
60
61
- plt .savefig (filepath , * args , ** kwargs )
61
+ fig = figure or plt .gcf ()
62
+ fig .savefig (filepath , * args , ** kwargs )
62
63
return filepath
63
64
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.
66
67
67
68
The plot that gets added is the one that would normally be shown when
68
69
using ``plt.show()``.
@@ -71,6 +72,8 @@ def add_plot(self, *args, extension='pdf', **kwargs):
71
72
----
72
73
args:
73
74
Arguments passed to plt.savefig for displaying the plot.
75
+ figure:
76
+ Optional matplotlib figure. If None add the current figure.
74
77
extension : str
75
78
extension of image file indicating figure file type
76
79
kwargs:
@@ -86,7 +89,9 @@ def add_plot(self, *args, extension='pdf', **kwargs):
86
89
if key in kwargs :
87
90
add_image_kwargs [key ] = kwargs .pop (key )
88
91
89
- filename = self ._save_plot (* args , extension = extension , ** kwargs )
92
+ filename = self ._save_plot (
93
+ * args , figure = figure , extension = extension , ** kwargs
94
+ )
90
95
91
96
self .add_image (filename , ** add_image_kwargs )
92
97
0 commit comments