Skip to content

Simplify logic for adding a single axes #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/napari_matplotlib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from typing import List, Tuple

import napari
from matplotlib.axes import Axes
from matplotlib.backends.backend_qt5agg import (
FigureCanvas,
NavigationToolbar2QT,
)
from matplotlib.figure import Figure
from qtpy.QtGui import QIcon
from qtpy.QtWidgets import QVBoxLayout, QWidget

Expand All @@ -23,6 +25,8 @@ class NapariMPLWidget(QWidget):
Base Matplotlib canvas. Widget that can be embedded as a napari widget.

This creates a single FigureCanvas, which contains a single Figure.
It is not responsible for creating any Axes, because different widgets
may want to implement different subplot layouts.

This class also handles callbacks to automatically update figures when
the layer selection or z-step is changed in the napari viewer. To take
Expand All @@ -33,8 +37,6 @@ class NapariMPLWidget(QWidget):
----------
viewer : `napari.Viewer`
Main napari viewer.
figure : `matplotlib.figure.Figure`
Matplotlib figure.
canvas : matplotlib.backends.backend_qt5agg.FigureCanvas
Matplotlib canvas.
layers : `list`
Expand Down Expand Up @@ -64,6 +66,11 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
# Accept any type of input layer by default
input_layer_types: Tuple[napari.layers.Layer, ...] = (napari.layers.Layer,)

@property
def figure(self) -> Figure:
"""Matplotlib figure."""
return self.canvas.figure

@property
def n_selected_layers(self) -> int:
"""
Expand Down Expand Up @@ -125,25 +132,31 @@ def draw(self) -> None:
This is a no-op, and is intended for derived classes to override.
"""

def apply_napari_colorscheme(self) -> None:
"""Apply napari-compatible colorscheme to the axes object."""
if self.axes is None:
return
def add_single_axes(self) -> None:
"""
Add a single Axes to the figure.

The Axes is saved on the ``.axes`` attribute for later access.
"""
self.axes = self.figure.subplots()
self.apply_napari_colorscheme(self.axes)

@staticmethod
def apply_napari_colorscheme(ax: Axes) -> None:
"""Apply napari-compatible colorscheme to an axes object."""
# changing color of axes background to transparent
self.canvas.figure.patch.set_facecolor("none")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The figure's facecolor doesn't need to be set?

No, it doesn't but I don't get why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already set in NapariMPLWidget.__init__()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 yep. Ty.

self.axes.set_facecolor("none")
ax.set_facecolor("none")

# changing colors of all axes
[
self.axes.spines[spine].set_color("white")
for spine in self.axes.spines
]
self.axes.xaxis.label.set_color("white")
self.axes.yaxis.label.set_color("white")
for spine in ax.spines:
ax.spines[spine].set_color("white")

ax.xaxis.label.set_color("white")
ax.yaxis.label.set_color("white")

# changing colors of axes labels
self.axes.tick_params(axis="x", colors="white")
self.axes.tick_params(axis="y", colors="white")
ax.tick_params(axis="x", colors="white")
ax.tick_params(axis="y", colors="white")

def _on_update_layers(self) -> None:
"""
Expand Down
3 changes: 1 addition & 2 deletions src/napari_matplotlib/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class HistogramWidget(NapariMPLWidget):

def __init__(self, napari_viewer: napari.viewer.Viewer):
super().__init__(napari_viewer)
self.axes = self.canvas.figure.subplots()
self.apply_napari_colorscheme()
self.add_single_axes()
self.update_layers(None)

def clear(self) -> None:
Expand Down
3 changes: 1 addition & 2 deletions src/napari_matplotlib/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class ScatterBaseWidget(NapariMPLWidget):
def __init__(self, napari_viewer: napari.viewer.Viewer):
super().__init__(napari_viewer)

self.axes = self.canvas.figure.subplots()
self.apply_napari_colorscheme()
self.add_single_axes()
self.update_layers(None)

def clear(self) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/napari_matplotlib/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SliceWidget(NapariMPLWidget):
def __init__(self, napari_viewer: napari.viewer.Viewer):
# Setup figure/axes
super().__init__(napari_viewer)
self.axes = self.canvas.figure.subplots()
self.add_single_axes()

button_layout = QHBoxLayout()
self.layout().addLayout(button_layout)
Expand Down