.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "advanced/mathematical_optimization/auto_examples/plot_compare_optimizers.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_compare_optimizers.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_advanced_mathematical_optimization_auto_examples_plot_compare_optimizers.py:


Plotting the comparison of optimizers
======================================

Plots the results from the comparison of optimizers.

.. GENERATED FROM PYTHON SOURCE LINES 8-106



.. image-sg:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_compare_optimizers_001.png
   :alt: plot compare optimizers
   :srcset: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_compare_optimizers_001.png
   :class: sphx-glr-single-img





.. code-block:: Python


    import pickle
    import sys

    import numpy as np

    import matplotlib
    import matplotlib.pyplot as plt

    results = pickle.load(
        open(f"helper/compare_optimizers_py{sys.version_info[0]}.pkl", "rb")
    )
    n_methods = len(list(results.values())[0]["Rosenbrock  "])
    n_dims = len(results)

    symbols = "o>*Ds"

    plt.figure(1, figsize=(10, 4))
    plt.clf()

    nipy_spectral = matplotlib.colormaps["nipy_spectral"]
    colors = nipy_spectral(np.linspace(0, 1, n_dims))[:, :3]

    method_names = list(list(results.values())[0]["Rosenbrock  "].keys())
    method_names.sort(key=lambda x: x[::-1], reverse=True)

    for n_dim_index, ((n_dim, n_dim_bench), color) in enumerate(
        zip(sorted(results.items()), colors, strict=True)
    ):
        for (cost_name, cost_bench), symbol in zip(
            sorted(n_dim_bench.items()), symbols, strict=True
        ):
            for (
                method_index,
                method_name,
            ) in enumerate(method_names):
                this_bench = cost_bench[method_name]
                bench = np.mean(this_bench)
                plt.semilogy(
                    [
                        method_index + 0.1 * n_dim_index,
                    ],
                    [
                        bench,
                    ],
                    marker=symbol,
                    color=color,
                )

    # Create a legend for the problem type
    for cost_name, symbol in zip(sorted(n_dim_bench.keys()), symbols, strict=True):
        plt.semilogy(
            [
                -10,
            ],
            [
                0,
            ],
            symbol,
            color=".5",
            label=cost_name,
        )

    plt.xticks(np.arange(n_methods), method_names, size=11)
    plt.xlim(-0.2, n_methods - 0.5)
    plt.legend(loc="best", numpoints=1, handletextpad=0, prop={"size": 12}, frameon=False)
    plt.ylabel("# function calls (a.u.)")

    # Create a second legend for the problem dimensionality
    plt.twinx()

    for n_dim, color in zip(sorted(results.keys()), colors, strict=True):
        plt.plot(
            [
                -10,
            ],
            [
                0,
            ],
            "o",
            color=color,
            label=f"# dim: {n_dim}",
        )
    plt.legend(
        loc=(0.47, 0.07),
        numpoints=1,
        handletextpad=0,
        prop={"size": 12},
        frameon=False,
        ncol=2,
    )
    plt.xlim(-0.2, n_methods - 0.5)

    plt.xticks(np.arange(n_methods), method_names)
    plt.yticks(())

    plt.tight_layout()
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.581 seconds)


.. _sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_compare_optimizers.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_compare_optimizers.ipynb <plot_compare_optimizers.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_compare_optimizers.py <plot_compare_optimizers.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_compare_optimizers.zip <plot_compare_optimizers.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_