Skip to content

Commit 0641069

Browse files
committed
Add how-to guide on changing plotting backends. Fix minor bugs in criterion plot
1 parent d5d5c65 commit 0641069

File tree

5 files changed

+192
-11
lines changed

5 files changed

+192
-11
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

docs/source/how_to/how_to_visualize_histories.ipynb

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"id": "8",
9898
"metadata": {},
9999
"source": [
100-
"## Use some advanced options of criterion_plot"
100+
"## Use some advanced options of criterion plot"
101101
]
102102
},
103103
{
@@ -121,14 +121,40 @@
121121
"cell_type": "markdown",
122122
"id": "10",
123123
"metadata": {},
124+
"source": [
125+
"## Criterion Plot with different plotting backends"
126+
]
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"id": "11",
131+
"metadata": {},
132+
"source": [
133+
"Also see: [How to change plotting backends](how_to_change_plotting_backend.ipynb) for a list on available backends, return types, and usage examples."
134+
]
135+
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"id": "12",
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"ax = om.criterion_plot(results, backend=\"matplotlib\")"
144+
]
145+
},
146+
{
147+
"cell_type": "markdown",
148+
"id": "13",
149+
"metadata": {},
124150
"source": [
125151
"## Make a params plot"
126152
]
127153
},
128154
{
129155
"cell_type": "code",
130156
"execution_count": null,
131-
"id": "11",
157+
"id": "14",
132158
"metadata": {},
133159
"outputs": [],
134160
"source": [
@@ -138,7 +164,7 @@
138164
},
139165
{
140166
"cell_type": "markdown",
141-
"id": "12",
167+
"id": "15",
142168
"metadata": {},
143169
"source": [
144170
"## Use advanced options of params plot"
@@ -147,7 +173,7 @@
147173
{
148174
"cell_type": "code",
149175
"execution_count": null,
150-
"id": "13",
176+
"id": "16",
151177
"metadata": {},
152178
"outputs": [],
153179
"source": [
@@ -163,16 +189,16 @@
163189
},
164190
{
165191
"cell_type": "markdown",
166-
"id": "14",
192+
"id": "17",
167193
"metadata": {},
168194
"source": [
169-
"## criterion_plot with multistart optimization"
195+
"## Criterion plot with multistart optimization"
170196
]
171197
},
172198
{
173199
"cell_type": "code",
174200
"execution_count": null,
175-
"id": "15",
201+
"id": "18",
176202
"metadata": {},
177203
"outputs": [],
178204
"source": [
@@ -192,7 +218,7 @@
192218
{
193219
"cell_type": "code",
194220
"execution_count": null,
195-
"id": "16",
221+
"id": "19",
196222
"metadata": {},
197223
"outputs": [],
198224
"source": [
@@ -203,7 +229,7 @@
203229
],
204230
"metadata": {
205231
"kernelspec": {
206-
"display_name": "Python 3 (ipykernel)",
232+
"display_name": "Python 3",
207233
"language": "python",
208234
"name": "python3"
209235
},
@@ -217,7 +243,7 @@
217243
"name": "python",
218244
"nbconvert_exporter": "python",
219245
"pygments_lexer": "ipython3",
220-
"version": "3.10.14"
246+
"version": "3.10.18"
221247
}
222248
},
223249
"nbformat": 4,

docs/source/how_to/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ how_to_constraints
1919
how_to_globalization
2020
how_to_multistart
2121
how_to_visualize_histories
22+
how_to_change_plotting_backend
2223
how_to_scaling
2324
how_to_logging
2425
how_to_errors_during_optimization

src/optimagic/visualization/backends.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def _line_plot_plotly(
4949
name=line.name,
5050
line_color=line.color,
5151
mode="lines",
52+
showlegend=line.show_in_legend,
5253
)
5354
fig.add_trace(trace)
5455

src/optimagic/visualization/history_plots.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def criterion_plot(
9595
# Generate the figure
9696

9797
fig = line_plot(
98-
lines=lines + multistart_lines,
98+
lines=multistart_lines + lines,
9999
backend=backend,
100100
xlabel="No. of criterion evaluations",
101101
ylabel="Criterion value",

0 commit comments

Comments
 (0)