.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "recipes/004_visualization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_recipes_004_visualization.py: .. _visualization: How to Implement Your Own Visualization Function =========================================================== In this section, we show how to implement your own visualization function. Here, we implement a visualization function that plots the optimization history. .. GENERATED FROM PYTHON SOURCE LINES 12-13 First of all, import `optuna` and other required modules. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python from __future__ import annotations import matplotlib import matplotlib.pyplot as plt import optuna import plotly.graph_objects as go .. GENERATED FROM PYTHON SOURCE LINES 23-24 If you use `plotly `__ for visualization, the function should return a ``plotly.graph_objects.Figure`` object. .. GENERATED FROM PYTHON SOURCE LINES 24-44 .. code-block:: Python def plot_optimizaiton_history(study: optuna.study.Study) -> go.Figure: trials = study.trials values = [trial.value for trial in trials] best_values = [min(values[: i + 1]) for i in range(len(values))] # type: ignore iterations = list(range(len(trials))) fig = go.Figure() fig.add_trace( go.Scatter( x=iterations, y=best_values, mode="lines+markers", ) ) fig.update_layout(title="Optimization history") return fig .. GENERATED FROM PYTHON SOURCE LINES 45-46 If you use `matplotlib `__ for visualization, the function should return a ``matplotlib.figure.Figure`` object. .. GENERATED FROM PYTHON SOURCE LINES 46-59 .. code-block:: Python def plot_optimizaiton_history_matplotlib(study: optuna.study.Study) -> matplotlib.figure.Figure: trials = study.trials values = [trial.value for trial in trials] best_values = [min(values[: i + 1]) for i in range(len(values))] # type: ignore fig, ax = plt.subplots() ax.set_title("Optimization history") ax.plot(best_values, marker="o") return fig .. GENERATED FROM PYTHON SOURCE LINES 60-62 Plot the optimization history using the implemented visualization function. Here, we use the simple quadratic function as objective function. .. GENERATED FROM PYTHON SOURCE LINES 62-76 .. code-block:: Python def objective(trial: optuna.trial.Trial) -> float: x = trial.suggest_float("x", -10, 10) return x**2 study = optuna.create_study() study.optimize(objective, n_trials=100) fig = plot_optimizaiton_history(study) fig.show() # plt.show() for matplotlib .. GENERATED FROM PYTHON SOURCE LINES 77-79 After implementing your own visualization function, you can register it with OptunaHub. See :doc:`002_registration` for how to register your visualization function with OptunaHub. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.377 seconds) .. _sphx_glr_download_recipes_004_visualization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 004_visualization.ipynb <004_visualization.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 004_visualization.py <004_visualization.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 004_visualization.zip <004_visualization.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_