@@ -38,6 +38,21 @@ def import_mpl_plt():
3838 return mpl , plt
3939
4040
41+ def _figure_to_html (fig : Figure ) -> str :
42+ """Convert a matplotlib Figure to an HTML img tag with base64-encoded PNG."""
43+ import base64
44+ import io
45+
46+ from matplotlib import pyplot as plt
47+
48+ buffer = io .BytesIO ()
49+ fig .savefig (buffer , format = 'png' , bbox_inches = 'tight' )
50+ plt .close (fig )
51+ buffer .seek (0 )
52+ img_str = base64 .b64encode (buffer .read ()).decode ('utf-8' )
53+ return f'<img src="data:image/png;base64,{ img_str } "/>'
54+
55+
4156def rotate (image : np .ndarray , * , radiological : bool = True , n : int = - 1 ) -> np .ndarray :
4257 # Rotate for visualization purposes
4358 image = np .rot90 (image , n , axes = (0 , 1 ))
@@ -214,8 +229,9 @@ def plot_subject(
214229 output_path = None ,
215230 figsize = None ,
216231 clear_axes = True ,
232+ savefig_kwargs : dict [str , Any ] | None = None ,
217233 ** plot_volume_kwargs ,
218- ):
234+ ) -> Figure :
219235 _ , plt = import_mpl_plt ()
220236 num_images = len (subject )
221237 many_images = num_images > 2
@@ -252,9 +268,12 @@ def plot_subject(
252268 axis .set_title (f'{ name } ({ axis_name } )' )
253269 plt .tight_layout ()
254270 if output_path is not None :
255- fig .savefig (output_path )
271+ if savefig_kwargs is None :
272+ savefig_kwargs = {}
273+ fig .savefig (output_path , ** savefig_kwargs )
256274 if show :
257275 plt .show ()
276+ return fig
258277
259278
260279def get_num_bins (x : np .ndarray ) -> int :
0 commit comments