|
| 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 the following visualization libraries as plotting backends:\n", |
| 17 | + "\n", |
| 18 | + "- [`plotly`](https://plotly.com/python/) (Default)\n", |
| 19 | + "- [`matplotlib`](https://matplotlib.org/)\n" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "2", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "To use a specific plotting backend, pass the backend name (e.g., `\"matplotlib\"`) to the `backend` argument.\n", |
| 28 | + "\n", |
| 29 | + "::::{tab-set}\n", |
| 30 | + ":sync-group: plotting_backend\n", |
| 31 | + "\n", |
| 32 | + ":::{tab-item} Plotly\n", |
| 33 | + ":sync: plotly\n", |
| 34 | + "`\"plotly\"`\n", |
| 35 | + ":::\n", |
| 36 | + "\n", |
| 37 | + ":::{tab-item} Matplotlib\n", |
| 38 | + ":sync: matplotlib\n", |
| 39 | + "`\"matplotlib\"`\n", |
| 40 | + ":::\n", |
| 41 | + "\n", |
| 42 | + "::::\n", |
| 43 | + "\n", |
| 44 | + "All plotting functions return the native figure object for the selected backend.\n", |
| 45 | + "\n", |
| 46 | + "::::{tab-set}\n", |
| 47 | + ":sync-group: plotting_backend\n", |
| 48 | + "\n", |
| 49 | + ":::{tab-item} Plotly\n", |
| 50 | + ":sync: plotly\n", |
| 51 | + "[`plotly.graph_objects.Figure`](https://plotly.com/python-api-reference/generated/plotly.graph_objects.Figure.html)\n", |
| 52 | + ":::\n", |
| 53 | + "\n", |
| 54 | + ":::{tab-item} Matplotlib\n", |
| 55 | + ":sync: matplotlib\n", |
| 56 | + "[`matplotlib.axes.Axes`](https://matplotlib.org/stable/api/axes_api.html#matplotlib.axes.Axes)\n", |
| 57 | + ":::\n", |
| 58 | + "\n", |
| 59 | + "::::" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "id": "3", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "import numpy as np\n", |
| 70 | + "import plotly.io as pio\n", |
| 71 | + "\n", |
| 72 | + "pio.renderers.default = \"notebook_connected\"\n", |
| 73 | + "%matplotlib inline\n", |
| 74 | + "\n", |
| 75 | + "import optimagic as om" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "4", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "def sphere(x):\n", |
| 86 | + " return x @ x\n", |
| 87 | + "\n", |
| 88 | + "\n", |
| 89 | + "results = {}\n", |
| 90 | + "for algo in [\"scipy_lbfgsb\", \"scipy_neldermead\"]:\n", |
| 91 | + " results[algo] = om.minimize(sphere, params=np.arange(5), algorithm=algo)" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "id": "5", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "## Criterion Plot with Plotly" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "markdown", |
| 104 | + "id": "6", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "By default, optimagic uses Plotly for plotting if you do not specify a backend." |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "id": "7", |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [], |
| 116 | + "source": [ |
| 117 | + "fig = om.criterion_plot(results)\n", |
| 118 | + "fig.show()" |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "markdown", |
| 123 | + "id": "8", |
| 124 | + "metadata": {}, |
| 125 | + "source": [ |
| 126 | + "## Criterion Plot with Matplotlib" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": null, |
| 132 | + "id": "9", |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [], |
| 135 | + "source": [ |
| 136 | + "ax = om.criterion_plot(results, backend=\"matplotlib\")" |
| 137 | + ] |
| 138 | + } |
| 139 | + ], |
| 140 | + "metadata": { |
| 141 | + "kernelspec": { |
| 142 | + "display_name": "optimagic", |
| 143 | + "language": "python", |
| 144 | + "name": "python3" |
| 145 | + }, |
| 146 | + "language_info": { |
| 147 | + "name": "python", |
| 148 | + "version": "3.10.17" |
| 149 | + } |
| 150 | + }, |
| 151 | + "nbformat": 4, |
| 152 | + "nbformat_minor": 5 |
| 153 | +} |
0 commit comments