1- from base64 import b64encode
2- from io import BytesIO
3-
41import napari
52import numpy as np
63from matplotlib .backends .backend_qt5agg import FigureCanvas
107__all__ = ["HistogramWidget" ]
118
129
13- def _figure_to_base64 (fig ):
14- # Converts a matplotlib Figure to a base64 UTF-8 string
15- buf = BytesIO ()
16- fig .savefig (
17- buf , format = "png" , facecolor = "none"
18- ) # works better than transparent=True
19- return b64encode (buf .getvalue ()).decode ("utf-8" )
20-
21-
2210class NapariMPLWidget (QWidget ):
2311 """
2412 Attributes
@@ -37,19 +25,13 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
3725 super ().__init__ ()
3826
3927 self .viewer = napari_viewer
40- self .figure = Figure (figsize = (5 , 3 ))
28+ self .figure = Figure (figsize = (5 , 3 ), tight_layout = True )
4129 self .canvas = FigureCanvas (self .figure )
4230 self .axes = self .canvas .figure .subplots ()
4331
4432 self .setLayout (QVBoxLayout ())
4533 self .layout ().addWidget (self .canvas )
4634
47- def _repr_html_ (self ):
48- buf = BytesIO ()
49- self .figure .savefig (buf , format = "png" , facecolor = "none" )
50- src = b64encode (buf .getvalue ()).decode ("utf-8" )
51- return f"<img src='data:image/png;base64,{ src } '/>"
52-
5335
5436class HistogramWidget (NapariMPLWidget ):
5537 """
0 commit comments