1
- from base64 import b64encode
2
- from io import BytesIO
3
-
4
1
import napari
5
2
import numpy as np
6
3
from matplotlib .backends .backend_qt5agg import FigureCanvas
10
7
__all__ = ["HistogramWidget" ]
11
8
12
9
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
-
22
10
class NapariMPLWidget (QWidget ):
23
11
"""
24
12
Attributes
@@ -37,19 +25,13 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
37
25
super ().__init__ ()
38
26
39
27
self .viewer = napari_viewer
40
- self .figure = Figure (figsize = (5 , 3 ))
28
+ self .figure = Figure (figsize = (5 , 3 ), tight_layout = True )
41
29
self .canvas = FigureCanvas (self .figure )
42
30
self .axes = self .canvas .figure .subplots ()
43
31
44
32
self .setLayout (QVBoxLayout ())
45
33
self .layout ().addWidget (self .canvas )
46
34
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
-
53
35
54
36
class HistogramWidget (NapariMPLWidget ):
55
37
"""
0 commit comments