differt.plotting.set_defaults

Contents

differt.plotting.set_defaults#

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

Set default backend and keyword arguments for future plotting utilities.

Previous defaults are ignored. To merge new defaults with the existing ones, use update_defaults.

Parameters:
  • backend (str | None) – The name of the backend to be passed to get_backend.

  • kwargs (Any) – Keyword arguments that will be passed to the corresponding process_*_kwargs function, and plot utilities.

Return type:

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

Returns:

The name of the (new) default backend.

Examples

The following example shows how to set the default plotting backend and other plotting defaults.

>>> 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("matplotlib")
... def _(*args, **kwargs):
...     dplt.process_matplotlib_kwargs(kwargs)
...     print(f"Using matplotlib backend with {args = }, {kwargs = }")
>>>
>>> my_plot()  # When not specified, use default backend
Using vispy backend with args = (), kwargs = {}
>>>
>>> dplt.set_defaults("matplotlib")  # We can change the default backend
'matplotlib'
>>> my_plot()  # So that now it defaults to 'matplotlib'
Using matplotlib backend with args = (), kwargs = {}
>>>
>>> dplt.set_defaults(
...     "matplotlib", color="red"
... )  # We can also specify additional defaults
'matplotlib'
>>> my_plot()  # So that now it defaults to 'matplotlib' and color='red'
Using matplotlib backend with args = (), kwargs = {'color': 'red'}
>>> my_plot(
...     backend="vispy"
... )  # Of course, the 'vispy' backend is still available
Using vispy backend with args = (), kwargs = {'color': 'red'}
>>> my_plot(
...     backend="vispy", color="green"
... )  # And we can also override any default
Using vispy backend with args = (), kwargs = {'color': 'green'}
>>> dplt.set_defaults("vispy")  # Reset all defaults
'vispy'