differt.plotting.use

Contents

differt.plotting.use#

use(backend=None, **kwargs)[source]#

Create a context manager that updates plotting defaults and returns the current default backend.

When exiting the context, the previous default backend and default keyword arguments are set back.

Parameters:
Yields:

The name of the default backend used in this context.

Return type:

Iterator[Literal['vispy', 'matplotlib', 'plotly']]

Examples

The following example shows how set plot defaults in a context.

>>> import differt.plotting as dplt
>>>
>>> @dplt.dispatch
... def my_plot(*args, **kwargs):
...     pass
>>>
>>> @my_plot.register("vispy")
... def _(*args, **kwargs):
...     dplt.process_vispy_kwargs(kwargs)
...     print(f"Using vispy backend with {args = }, {kwargs = }")
>>>
>>> @my_plot.register("plotly")
... def _(*args, **kwargs):
...     dplt.process_plotly_kwargs(kwargs)
...     print(f"Using plotly backend with {args = }, {kwargs = }")
>>>
>>> my_plot()  # When not specified, use default backend
Using vispy backend with args = (), kwargs = {}
>>> with (
...     dplt.use()
... ):  # No parameters = reset defaults (except the default backend)
...     my_plot()
Using vispy backend with args = (), kwargs = {}
>>> with dplt.use("plotly"):  # We can change the default backend
...     my_plot()  # So that now it defaults to 'matplotlib'
Using plotly backend with args = (), kwargs = {}
>>>
>>> with dplt.use(
...     "plotly", color="black"
... ):  # We can also specify additional defaults
...     my_plot()
Using plotly backend with args = (), kwargs = {'color': 'black'}