Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4e7cf8b

Browse filesBrowse files
committed
Simplify logic for adding a single axes
1 parent 6b99b20 commit 4e7cf8b
Copy full SHA for 4e7cf8b

File tree

Expand file treeCollapse file tree

4 files changed

+32
-21
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+32
-21
lines changed

‎src/napari_matplotlib/base.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/base.py
+29-16Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from typing import List, Tuple
44

55
import napari
6+
from matplotlib.axes import Axes
67
from matplotlib.backends.backend_qt5agg import (
78
FigureCanvas,
89
NavigationToolbar2QT,
910
)
11+
from matplotlib.figure import Figure
1012
from qtpy.QtGui import QIcon
1113
from qtpy.QtWidgets import QVBoxLayout, QWidget
1214

@@ -23,6 +25,8 @@ class NapariMPLWidget(QWidget):
2325
Base Matplotlib canvas. Widget that can be embedded as a napari widget.
2426
2527
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.
2630
2731
This class also handles callbacks to automatically update figures when
2832
the layer selection or z-step is changed in the napari viewer. To take
@@ -33,8 +37,6 @@ class NapariMPLWidget(QWidget):
3337
----------
3438
viewer : `napari.Viewer`
3539
Main napari viewer.
36-
figure : `matplotlib.figure.Figure`
37-
Matplotlib figure.
3840
canvas : matplotlib.backends.backend_qt5agg.FigureCanvas
3941
Matplotlib canvas.
4042
layers : `list`
@@ -64,6 +66,11 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
6466
# Accept any type of input layer by default
6567
input_layer_types: Tuple[napari.layers.Layer, ...] = (napari.layers.Layer,)
6668

69+
@property
70+
def figure(self) -> Figure:
71+
"""Matplotlib figure."""
72+
return self.canvas.figure
73+
6774
@property
6875
def n_selected_layers(self) -> int:
6976
"""
@@ -125,25 +132,31 @@ def draw(self) -> None:
125132
This is a no-op, and is intended for derived classes to override.
126133
"""
127134

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."""
132147
# 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")
135149

136150
# 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")
143156

144157
# 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")
147160

148161
def _on_update_layers(self) -> None:
149162
"""

‎src/napari_matplotlib/histogram.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/histogram.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ class HistogramWidget(NapariMPLWidget):
2121

2222
def __init__(self, napari_viewer: napari.viewer.Viewer):
2323
super().__init__(napari_viewer)
24-
self.axes = self.canvas.figure.subplots()
25-
self.apply_napari_colorscheme()
24+
self.add_single_axes()
2625
self.update_layers(None)
2726

2827
def clear(self) -> None:

‎src/napari_matplotlib/scatter.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/scatter.py
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ class ScatterBaseWidget(NapariMPLWidget):
3131
def __init__(self, napari_viewer: napari.viewer.Viewer):
3232
super().__init__(napari_viewer)
3333

34-
self.axes = self.canvas.figure.subplots()
35-
self.apply_napari_colorscheme()
34+
self.add_single_axes()
3635
self.update_layers(None)
3736

3837
def clear(self) -> None:

‎src/napari_matplotlib/slice.py

Copy file name to clipboardExpand all lines: src/napari_matplotlib/slice.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SliceWidget(NapariMPLWidget):
2424
def __init__(self, napari_viewer: napari.viewer.Viewer):
2525
# Setup figure/axes
2626
super().__init__(napari_viewer)
27-
self.axes = self.canvas.figure.subplots()
27+
self.add_single_axes()
2828

2929
button_layout = QHBoxLayout()
3030
self.layout().addLayout(button_layout)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.