Skip to content

Theme detection and update improvements #237

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 8 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Clean out old styling code
  • Loading branch information
dstansby committed Jan 11, 2024
commit 5fd84189d8f6589ecafe4dc31d7a3d40e1153f6c
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include LICENSE
include README.md
recursive-include * *.mplstyle

recursive-exclude * __pycache__
recursive-exclude * *.py[co]
Expand Down
11 changes: 3 additions & 8 deletions docs/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ To use these:

Customising plots
-----------------
`Matplotlib style sheets <https://matplotlib.org/stable/tutorials/introductory/customizing.html#defining-your-own-style>`__ can be used to customise
the plots generated by ``napari-matplotlib``.
To use a custom style sheet:

1. Save it as ``napari-matplotlib.mplstyle``
2. Put it in the Matplotlib configuration directory.
The location of this directory varies on different computers,
and can be found by calling :func:`matplotlib.get_configdir()`.
``napari-matplotlib`` uses colours from the current napari theme to customise the
Matplotlib plots. See `the example on creating a new napari theme
<https://napari.org/stable/gallery/new_theme.html>`_ for a helpful guide.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ write_to = "src/napari_matplotlib/_version.py"

[tool.pytest.ini_options]
qt_api = "pyqt6"
addopts = "--mpl"
addopts = "--mpl --mpl-baseline-relative"
filterwarnings = [
"error",
# Coming from vispy
Expand Down
6 changes: 0 additions & 6 deletions src/napari_matplotlib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pathlib import Path
from typing import Optional

import matplotlib
import matplotlib.style as mplstyle
import napari
from matplotlib.backends.backend_qtagg import ( # type: ignore[attr-defined]
Expand All @@ -19,10 +18,6 @@

__all__ = ["BaseNapariMPLWidget", "NapariMPLWidget", "SingleAxesWidget"]

_CUSTOM_STYLE_PATH = (
Path(matplotlib.get_configdir()) / "napari-matplotlib.mplstyle"
)


class BaseNapariMPLWidget(QWidget):
"""
Expand All @@ -47,7 +42,6 @@ def __init__(
):
super().__init__(parent=parent)
self.viewer = napari_viewer
# self._mpl_style_sheet_path: Optional[Path] = None
self.napari_theme_style_sheet = style_sheet_from_theme(
get_theme(napari_viewer.theme, as_dict=False)
)
Expand Down
3 changes: 0 additions & 3 deletions src/napari_matplotlib/styles/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions src/napari_matplotlib/styles/dark.mplstyle

This file was deleted.

12 changes: 0 additions & 12 deletions src/napari_matplotlib/styles/light.mplstyle

This file was deleted.

15 changes: 0 additions & 15 deletions src/napari_matplotlib/tests/data/test_theme.mplstyle

This file was deleted.

71 changes: 1 addition & 70 deletions src/napari_matplotlib/tests/test_theme.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import os
import shutil
from copy import deepcopy
from pathlib import Path

import matplotlib
import napari
import numpy as np
import pytest
from matplotlib.colors import to_rgba

from napari_matplotlib import HistogramWidget, ScatterWidget
from napari_matplotlib import ScatterWidget
from napari_matplotlib.base import NapariMPLWidget


Expand Down Expand Up @@ -127,66 +121,3 @@ def test_no_theme_side_effects(make_napari_viewer):
unrelated_figure.tight_layout()

return unrelated_figure


@pytest.mark.mpl_image_compare
def test_custom_theme(make_napari_viewer, theme_path, brain_data):
viewer = make_napari_viewer()
viewer.theme = "dark"

widget = ScatterWidget(viewer)
widget.mpl_style_sheet_path = theme_path

viewer.add_image(brain_data[0], **brain_data[1], name="brain")
viewer.add_image(
brain_data[0] * -1, **brain_data[1], name="brain_reversed"
)

viewer.layers.selection.clear()
viewer.layers.selection.add(viewer.layers[0])
viewer.layers.selection.add(viewer.layers[1])

return deepcopy(widget.figure)


def find_mpl_stylesheet(name: str) -> Path:
"""Find the built-in matplotlib stylesheet."""
return Path(matplotlib.__path__[0]) / f"mpl-data/stylelib/{name}.mplstyle"


def test_custom_stylesheet(make_napari_viewer, image_data):
"""
Test that a stylesheet in the current directory is given precidence.

Do this by copying over a stylesheet from matplotlib's built in styles,
naming it correctly, and checking the colours are as expected.
"""
# Copy Solarize_Light2 as if it was a user-overriden stylesheet.
style_sheet_path = (
Path(matplotlib.get_configdir()) / "napari-matplotlib.mplstyle"
)
if style_sheet_path.exists():
pytest.skip("Won't ovewrite existing custom style sheet.")
shutil.copy(
find_mpl_stylesheet("Solarize_Light2"),
style_sheet_path,
)

try:
viewer = make_napari_viewer()
viewer.add_image(image_data[0], **image_data[1])
widget = HistogramWidget(viewer)
assert widget.mpl_style_sheet_path == style_sheet_path
ax = widget.figure.gca()

# The axes should have a light brownish grey background:
assert ax.get_facecolor() == to_rgba("#eee8d5")
assert ax.patch.get_facecolor() == to_rgba("#eee8d5")

# The figure background and axis gridlines are light yellow:
assert widget.figure.patch.get_facecolor() == to_rgba("#fdf6e3")
for gridline in ax.get_xgridlines() + ax.get_ygridlines():
assert gridline.get_visible() is True
assert gridline.get_color() == "#b0b0b0"
finally:
os.remove(style_sheet_path)