@@ -22,7 +22,7 @@ def add_image(
22
22
filename ,
23
23
* ,
24
24
width = NoEscape (r"0.8\textwidth" ),
25
- placement = NoEscape (r"\centering" )
25
+ placement = NoEscape (r"\centering" ),
26
26
):
27
27
"""Add an image to the figure.
28
28
@@ -50,7 +50,7 @@ def add_image(
50
50
StandAloneGraphic (image_options = width , filename = fix_filename (filename ))
51
51
)
52
52
53
- def _save_plot (self , * args , extension = "pdf" , ** kwargs ):
53
+ def _save_plot (self , * args , figure = None , extension = "pdf" , ** kwargs ):
54
54
"""Save the plot.
55
55
56
56
Returns
@@ -64,11 +64,12 @@ def _save_plot(self, *args, extension="pdf", **kwargs):
64
64
filename = "{}.{}" .format (str (uuid .uuid4 ()), extension .strip ("." ))
65
65
filepath = posixpath .join (tmp_path , filename )
66
66
67
- plt .savefig (filepath , * args , ** kwargs )
67
+ fig = figure or plt .gcf ()
68
+ fig .savefig (filepath , * args , ** kwargs )
68
69
return filepath
69
70
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.
72
73
73
74
The plot that gets added is the one that would normally be shown when
74
75
using ``plt.show()``.
@@ -77,6 +78,8 @@ def add_plot(self, *args, extension="pdf", **kwargs):
77
78
----
78
79
args:
79
80
Arguments passed to plt.savefig for displaying the plot.
81
+ figure:
82
+ Optional matplotlib figure. If None add the current figure.
80
83
extension : str
81
84
extension of image file indicating figure file type
82
85
kwargs:
@@ -92,7 +95,7 @@ def add_plot(self, *args, extension="pdf", **kwargs):
92
95
if key in kwargs :
93
96
add_image_kwargs [key ] = kwargs .pop (key )
94
97
95
- filename = self ._save_plot (* args , extension = extension , ** kwargs )
98
+ filename = self ._save_plot (* args , figure = figure , extension = extension , ** kwargs )
96
99
97
100
self .add_image (filename , ** add_image_kwargs )
98
101
0 commit comments