Skip to content

Commit 355d0f2

Browse files
committed
ENH(plot): accept bare dict for Theme overrides
1 parent 02a0663 commit 355d0f2

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

docs/source/pipelines.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ into the plot:
9090

9191
.. graphtik::
9292

93-
>>> from graphtik.plot import Theme
94-
95-
>>> dot = out.plot(theme=Theme(include_steps=True))
93+
>>> dot = out.plot(theme={"include_steps": True})
9694

9795
.. tip:
9896
Read :ref:`plot-customizations` to understand the trick with the :term:`plotter`.

docs/source/plotting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ ordered by breadth of the effects (most broadly effecting method at the top):
141141
customizations::
142142

143143
active_plotter = get_active_plotter()
144-
netop.plot(theme=active_plotter.default_theme.withset(include_steps=True))
144+
netop.plot(theme={"include_steps": True})
145145

146146
... OR::
147147

graphtik/base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import abc
1616
import inspect
1717
import logging
18+
from collections import abc as cabc
1819
from collections import defaultdict, namedtuple
1920
from functools import partial, partialmethod, wraps
2021
from typing import Any, Collection, List, Mapping, NamedTuple, Optional, Tuple, Union
@@ -397,6 +398,7 @@ class PlotArgs(NamedTuple):
397398
#: If given, overrides :active plotter`.
398399
plotter: "graphtik.plot.Plotter" = None
399400
#: If given, overrides :term:`plot theme` plotter will use.
401+
#: It can be any mapping, in which case it overrite the :term:`current theme`.
400402
theme: "graphtik.plot.Theme" = None
401403

402404
#######
@@ -509,8 +511,8 @@ def plot(
509511
510512
:seealso: :attr:`.PlotArgs.plotter`
511513
:param theme:
512-
Any :term:`plot theme` overrides; if none, the :attr:`.Plotter.default_theme`
513-
of the :term:`active plotter` is used.
514+
Any :term:`plot theme` or dictionary overrides; if none,
515+
the :attr:`.Plotter.default_theme` of the :term:`active plotter` is used.
514516
515517
:seealso: :attr:`.PlotArgs.theme`
516518
:param name:
@@ -697,11 +699,21 @@ def plot(
697699

698700
plot_args = PlotArgs(**kw)
699701

700-
from .plot import Plotter, get_active_plotter
702+
from .plot import Plotter, Theme, get_active_plotter
701703

704+
## Ensure a valid plotter in the args asap.
705+
#
702706
if plotter and not isinstance(plotter, Plotter):
703707
raise TypeError(f"Invalid `plotter` argument given: {plotter}")
704-
plot_args = plot_args._replace(plotter=plotter or get_active_plotter())
708+
if not plotter:
709+
plotter = get_active_plotter()
710+
plot_args = plot_args._replace(plotter=plotter)
711+
712+
## Overwrite any dictionaries over active theme asap.
713+
#
714+
if isinstance(theme, cabc.Mapping):
715+
theme = plotter.default_theme.withset(**theme)
716+
plot_args = plot_args._replace(theme=theme)
705717

706718
plot_args = self.prepare_plot_args(plot_args)
707719
assert plot_args.graph, plot_args

0 commit comments

Comments
 (0)