How to change the plotting backend

optimagic supports various visualization libraries as plotting backends, which can be selected using the backend argument.

The default plotting library. To select the Plotly backend explicitly, set backend="plotly".

The returned figure object is a plotly.graph_objects.Figure.

To select the Matplotlib backend, set backend="matplotlib".

The returned figure object is a matplotlib.axes.Axes.

In the following guide, we showcase the criterion plot visualized using different backends.

import numpy as np

import optimagic as om


def sphere(x):
    return x @ x


results = {}
for algo in ["scipy_lbfgsb", "scipy_neldermead"]:
    results[algo] = om.minimize(sphere, params=np.arange(5), algorithm=algo)

Plotly

Note

Choose the Plotly renderer according to your environment:

  • Use plotly.io.renderers.default = "notebook_connected" in Jupyter notebooks for interactive plots.

  • Use plotly.io.renderers.default = "browser" to open plots in your default web browser when running as a script.

Refer to the Plotly documentation for more details.

import plotly.io as pio

pio.renderers.default = "notebook_connected"

fig = om.criterion_plot(results, backend="plotly")  # Also the default
fig.show()

Matplotlib

ax = om.criterion_plot(results, backend="matplotlib")
../_images/850458f5224f9e6940786155cfcafb9630c914516eb17cc0d7bfa71faa33d0b2.png