|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# How to change the plotting backend" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "1", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "optimagic supports various visualization libraries as plotting backends, which can be\n", |
| 17 | + "selected using the `backend` argument." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "id": "2", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "::::{tab-set}\n", |
| 26 | + "\n", |
| 27 | + ":::{tab-item} Plotly\n", |
| 28 | + "\n", |
| 29 | + "The default plotting library. To select the Plotly backend explicitly, set `backend=\"plotly\"`.\n", |
| 30 | + "\n", |
| 31 | + "The returned figure object is a [`plotly.graph_objects.Figure`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html).\n", |
| 32 | + "\n", |
| 33 | + ":::\n", |
| 34 | + "\n", |
| 35 | + ":::{tab-item} Matplotlib\n", |
| 36 | + "\n", |
| 37 | + "To select the Matplotlib backend, set `backend=\"matplotlib\"`.\n", |
| 38 | + "\n", |
| 39 | + "The returned figure object is a [`matplotlib.axes.Axes`](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html).\n", |
| 40 | + "\n", |
| 41 | + ":::\n", |
| 42 | + "\n", |
| 43 | + "::::" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "id": "3", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "In the following guide, we showcase the criterion plot visualized using different\n", |
| 52 | + "backends." |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "id": "4", |
| 59 | + "metadata": {}, |
| 60 | + "outputs": [], |
| 61 | + "source": [ |
| 62 | + "import numpy as np\n", |
| 63 | + "\n", |
| 64 | + "import optimagic as om\n", |
| 65 | + "\n", |
| 66 | + "\n", |
| 67 | + "def sphere(x):\n", |
| 68 | + " return x @ x\n", |
| 69 | + "\n", |
| 70 | + "\n", |
| 71 | + "results = {}\n", |
| 72 | + "for algo in [\"scipy_lbfgsb\", \"scipy_neldermead\"]:\n", |
| 73 | + " results[algo] = om.minimize(sphere, params=np.arange(5), algorithm=algo)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "id": "5", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "## Plotly" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "6", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + ":::{note}\n", |
| 90 | + "\n", |
| 91 | + "**Choose the Plotly renderer according to your environment:**\n", |
| 92 | + "\n", |
| 93 | + "- Use `plotly.io.renderers.default = \"notebook_connected\"` in Jupyter notebooks for interactive plots.\n", |
| 94 | + "- Use `plotly.io.renderers.default = \"browser\"` to open plots in your default web browser when running as a script.\n", |
| 95 | + "\n", |
| 96 | + "Refer to the [Plotly documentation](https://plotly.com/python/renderers/) for more details.\n", |
| 97 | + "\n", |
| 98 | + ":::" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "id": "7", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "import plotly.io as pio\n", |
| 109 | + "\n", |
| 110 | + "pio.renderers.default = \"notebook_connected\"\n", |
| 111 | + "\n", |
| 112 | + "fig = om.criterion_plot(results, backend=\"plotly\") # Also the default\n", |
| 113 | + "fig.show()" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "id": "8", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "## Matplotlib" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "id": "9", |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "ax = om.criterion_plot(results, backend=\"matplotlib\")" |
| 132 | + ] |
| 133 | + } |
| 134 | + ], |
| 135 | + "metadata": { |
| 136 | + "kernelspec": { |
| 137 | + "display_name": "Python 3", |
| 138 | + "language": "python", |
| 139 | + "name": "python3" |
| 140 | + }, |
| 141 | + "language_info": { |
| 142 | + "codemirror_mode": { |
| 143 | + "name": "ipython", |
| 144 | + "version": 3 |
| 145 | + }, |
| 146 | + "file_extension": ".py", |
| 147 | + "mimetype": "text/x-python", |
| 148 | + "name": "python", |
| 149 | + "nbconvert_exporter": "python", |
| 150 | + "pygments_lexer": "ipython3", |
| 151 | + "version": "3.10.18" |
| 152 | + } |
| 153 | + }, |
| 154 | + "nbformat": 4, |
| 155 | + "nbformat_minor": 5 |
| 156 | +} |
0 commit comments