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:
backend (
str|None) – The name of the backend to be passed toget_backend.kwargs (
Any) – Keywords arguments passed toupdate_defaults.
- Yields:
The name of the default backend used in this context.
- Return type:
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'}