3
3
from typing import List , Tuple
4
4
5
5
import napari
6
+ from matplotlib .axes import Axes
6
7
from matplotlib .backends .backend_qt5agg import (
7
8
FigureCanvas ,
8
9
NavigationToolbar2QT ,
9
10
)
11
+ from matplotlib .figure import Figure
10
12
from qtpy .QtGui import QIcon
11
13
from qtpy .QtWidgets import QVBoxLayout , QWidget
12
14
@@ -23,6 +25,8 @@ class NapariMPLWidget(QWidget):
23
25
Base Matplotlib canvas. Widget that can be embedded as a napari widget.
24
26
25
27
This creates a single FigureCanvas, which contains a single Figure.
28
+ It is not responsible for creating any Axes, because different widgets
29
+ may want to implement different subplot layouts.
26
30
27
31
This class also handles callbacks to automatically update figures when
28
32
the layer selection or z-step is changed in the napari viewer. To take
@@ -33,8 +37,6 @@ class NapariMPLWidget(QWidget):
33
37
----------
34
38
viewer : `napari.Viewer`
35
39
Main napari viewer.
36
- figure : `matplotlib.figure.Figure`
37
- Matplotlib figure.
38
40
canvas : matplotlib.backends.backend_qt5agg.FigureCanvas
39
41
Matplotlib canvas.
40
42
layers : `list`
@@ -64,6 +66,11 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
64
66
# Accept any type of input layer by default
65
67
input_layer_types : Tuple [napari .layers .Layer , ...] = (napari .layers .Layer ,)
66
68
69
+ @property
70
+ def figure (self ) -> Figure :
71
+ """Matplotlib figure."""
72
+ return self .canvas .figure
73
+
67
74
@property
68
75
def n_selected_layers (self ) -> int :
69
76
"""
@@ -125,25 +132,31 @@ def draw(self) -> None:
125
132
This is a no-op, and is intended for derived classes to override.
126
133
"""
127
134
128
- def apply_napari_colorscheme (self ) -> None :
129
- """Apply napari-compatible colorscheme to the axes object."""
130
- if self .axes is None :
131
- return
135
+ def add_single_axes (self ) -> None :
136
+ """
137
+ Add a single Axes to the figure.
138
+
139
+ The Axes is saved on the ``.axes`` attribute for later access.
140
+ """
141
+ self .axes = self .figure .subplots ()
142
+ self .apply_napari_colorscheme (self .axes )
143
+
144
+ @staticmethod
145
+ def apply_napari_colorscheme (ax : Axes ) -> None :
146
+ """Apply napari-compatible colorscheme to an axes object."""
132
147
# changing color of axes background to transparent
133
- self .canvas .figure .patch .set_facecolor ("none" )
134
- self .axes .set_facecolor ("none" )
148
+ ax .set_facecolor ("none" )
135
149
136
150
# changing colors of all axes
137
- [
138
- self .axes .spines [spine ].set_color ("white" )
139
- for spine in self .axes .spines
140
- ]
141
- self .axes .xaxis .label .set_color ("white" )
142
- self .axes .yaxis .label .set_color ("white" )
151
+ for spine in ax .spines :
152
+ ax .spines [spine ].set_color ("white" )
153
+
154
+ ax .xaxis .label .set_color ("white" )
155
+ ax .yaxis .label .set_color ("white" )
143
156
144
157
# changing colors of axes labels
145
- self . axes .tick_params (axis = "x" , colors = "white" )
146
- self . axes .tick_params (axis = "y" , colors = "white" )
158
+ ax .tick_params (axis = "x" , colors = "white" )
159
+ ax .tick_params (axis = "y" , colors = "white" )
147
160
148
161
def _on_update_layers (self ) -> None :
149
162
"""
0 commit comments