|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "5968b66a-8e78-488e-a2a0-1b2ac72cbb28", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# GridPlot\n", |
| 9 | + "\n", |
| 10 | + "Allows for proper subplotting. Create graphics and add them to a `GridPlot`." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "66eaaf8e-3324-4e77-9cc4-f52682c7caea", |
| 17 | + "metadata": { |
| 18 | + "tags": [] |
| 19 | + }, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import fastplotlib as fpl\n", |
| 23 | + "import numpy as np\n", |
| 24 | + "from sidecar import Sidecar" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": 2, |
| 30 | + "id": "e481359b-6b49-40c5-86dd-b1c177314cd6", |
| 31 | + "metadata": { |
| 32 | + "tags": [] |
| 33 | + }, |
| 34 | + "outputs": [ |
| 35 | + { |
| 36 | + "data": { |
| 37 | + "application/vnd.jupyter.widget-view+json": { |
| 38 | + "model_id": "01f8aa22c8c94c8a81eba580d256bb4b", |
| 39 | + "version_major": 2, |
| 40 | + "version_minor": 0 |
| 41 | + }, |
| 42 | + "text/plain": [ |
| 43 | + "RFBOutputContext()" |
| 44 | + ] |
| 45 | + }, |
| 46 | + "metadata": {}, |
| 47 | + "output_type": "display_data" |
| 48 | + }, |
| 49 | + { |
| 50 | + "name": "stderr", |
| 51 | + "output_type": "stream", |
| 52 | + "text": [ |
| 53 | + "/home/clewis7/repos/fastplotlib/fastplotlib/graphics/_features/_base.py:34: UserWarning: converting float64 array to float32\n", |
| 54 | + " warn(f\"converting {array.dtype} array to float32\")\n" |
| 55 | + ] |
| 56 | + } |
| 57 | + ], |
| 58 | + "source": [ |
| 59 | + "# GridPlot of shape 2 x 3 with all controllers synced\n", |
| 60 | + "grid_plot = fpl.GridPlot(shape=(2, 3), controllers=\"sync\")\n", |
| 61 | + "\n", |
| 62 | + "# Make a random image graphic for each subplot\n", |
| 63 | + "for subplot in grid_plot:\n", |
| 64 | + " # create image data\n", |
| 65 | + " data = np.random.rand(512, 512)\n", |
| 66 | + " # add an image to the subplot\n", |
| 67 | + " subplot.add_image(data, name=\"rand-img\")\n", |
| 68 | + "\n", |
| 69 | + "# Define a function to update the image graphics with new data\n", |
| 70 | + "# add_animations will pass the gridplot to the animation function\n", |
| 71 | + "def update_data(gp):\n", |
| 72 | + " for sp in gp:\n", |
| 73 | + " new_data = np.random.rand(512, 512)\n", |
| 74 | + " # index the image graphic by name and set the data\n", |
| 75 | + " sp[\"rand-img\"].data = new_data\n", |
| 76 | + " \n", |
| 77 | + "# add the animation function\n", |
| 78 | + "grid_plot.add_animations(update_data)\n", |
| 79 | + "\n", |
| 80 | + "sc = Sidecar(title=\"gridplot\")\n", |
| 81 | + "\n", |
| 82 | + "# show the gridplot \n", |
| 83 | + "with sc:\n", |
| 84 | + " display(grid_plot.show())" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "id": "30596ad1-ffb7-4beb-a723-7cb5a2f45eac", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "## Accessing subplots within `GridPlot`" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 3, |
| 98 | + "id": "ed86a89d-0cb9-44e6-97ac-56499c9a0a2e", |
| 99 | + "metadata": { |
| 100 | + "tags": [] |
| 101 | + }, |
| 102 | + "outputs": [ |
| 103 | + { |
| 104 | + "data": { |
| 105 | + "text/plain": [ |
| 106 | + "unnamed: Subplot @ 0x7fac550680d0\n", |
| 107 | + " parent: fastplotlib.GridPlot @ 0x7fac550a0d10\n", |
| 108 | + "\n", |
| 109 | + " Graphics:\n", |
| 110 | + "\t'rand-img': ImageGraphic @ 0x7fac3c169310" |
| 111 | + ] |
| 112 | + }, |
| 113 | + "execution_count": 3, |
| 114 | + "metadata": {}, |
| 115 | + "output_type": "execute_result" |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "# positional indexing\n", |
| 120 | + "# row 0 and col 0\n", |
| 121 | + "grid_plot[0, 0]" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "id": "cd073684-8f3a-4283-b9c1-aad271c82752", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "### You can get the graphics within a subplot, just like with simple `Plot`" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 4, |
| 135 | + "id": "bf671e47-70b7-428b-bfca-55123e99344d", |
| 136 | + "metadata": { |
| 137 | + "tags": [] |
| 138 | + }, |
| 139 | + "outputs": [ |
| 140 | + { |
| 141 | + "data": { |
| 142 | + "text/plain": [ |
| 143 | + "(<weakproxy at 0x7fac0472ca90 to ImageGraphic at 0x7fac04725810>,)" |
| 144 | + ] |
| 145 | + }, |
| 146 | + "execution_count": 4, |
| 147 | + "metadata": {}, |
| 148 | + "output_type": "execute_result" |
| 149 | + } |
| 150 | + ], |
| 151 | + "source": [ |
| 152 | + "grid_plot[0, 1].graphics" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "id": "25a0ef00-05f3-484a-9b38-91d72104e736", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "### and change their properties" |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "code", |
| 165 | + "execution_count": 5, |
| 166 | + "id": "2b9f4996-9321-488c-aa7d-99129a8a0dd0", |
| 167 | + "metadata": { |
| 168 | + "tags": [] |
| 169 | + }, |
| 170 | + "outputs": [], |
| 171 | + "source": [ |
| 172 | + "grid_plot[0, 1].graphics[0].cmap.vmax = 0.5" |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "markdown", |
| 177 | + "id": "5337a9cf-4823-43f3-9fa5-9641f4026d50", |
| 178 | + "metadata": {}, |
| 179 | + "source": [ |
| 180 | + "### more indexing with `GridPlot`" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "code", |
| 185 | + "execution_count": 6, |
| 186 | + "id": "fb186973-1ce2-4361-bb97-0f50342f9861", |
| 187 | + "metadata": { |
| 188 | + "tags": [] |
| 189 | + }, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "# you can give subplots human-readable string names\n", |
| 193 | + "grid_plot[0, 2].name = \"top-right-plot\"" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 7, |
| 199 | + "id": "403d2d0a-7cbf-4e43-a75a-dfff98d40cf2", |
| 200 | + "metadata": { |
| 201 | + "tags": [] |
| 202 | + }, |
| 203 | + "outputs": [ |
| 204 | + { |
| 205 | + "data": { |
| 206 | + "text/plain": [ |
| 207 | + "top-right-plot: Subplot @ 0x7fac3c092e50\n", |
| 208 | + " parent: fastplotlib.GridPlot @ 0x7fac550a0d10\n", |
| 209 | + "\n", |
| 210 | + " Graphics:\n", |
| 211 | + "\t'rand-img': ImageGraphic @ 0x7fac04727310" |
| 212 | + ] |
| 213 | + }, |
| 214 | + "execution_count": 7, |
| 215 | + "metadata": {}, |
| 216 | + "output_type": "execute_result" |
| 217 | + } |
| 218 | + ], |
| 219 | + "source": [ |
| 220 | + "grid_plot[\"top-right-plot\"]" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 8, |
| 226 | + "id": "4f296891-a8b4-47d1-8d17-7554c36b00ed", |
| 227 | + "metadata": { |
| 228 | + "tags": [] |
| 229 | + }, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "data": { |
| 233 | + "text/plain": [ |
| 234 | + "(0, 2)" |
| 235 | + ] |
| 236 | + }, |
| 237 | + "execution_count": 8, |
| 238 | + "metadata": {}, |
| 239 | + "output_type": "execute_result" |
| 240 | + } |
| 241 | + ], |
| 242 | + "source": [ |
| 243 | + "# view its position\n", |
| 244 | + "grid_plot[\"top-right-plot\"].position" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 9, |
| 250 | + "id": "85f06940-3eec-49e9-97a0-00054ad27d8a", |
| 251 | + "metadata": { |
| 252 | + "tags": [] |
| 253 | + }, |
| 254 | + "outputs": [ |
| 255 | + { |
| 256 | + "data": { |
| 257 | + "text/plain": [ |
| 258 | + "True" |
| 259 | + ] |
| 260 | + }, |
| 261 | + "execution_count": 9, |
| 262 | + "metadata": {}, |
| 263 | + "output_type": "execute_result" |
| 264 | + } |
| 265 | + ], |
| 266 | + "source": [ |
| 267 | + "# these are really the same\n", |
| 268 | + "grid_plot[\"top-right-plot\"] is grid_plot[0, 2]" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "markdown", |
| 273 | + "id": "33ced14b-f958-474e-85a9-5f395a8de82b", |
| 274 | + "metadata": {}, |
| 275 | + "source": [ |
| 276 | + "Indexing with subplot name and graphic name" |
| 277 | + ] |
| 278 | + }, |
| 279 | + { |
| 280 | + "cell_type": "code", |
| 281 | + "execution_count": 10, |
| 282 | + "id": "75ecdd19-bd9a-4d93-bb18-a6216e3fde1b", |
| 283 | + "metadata": { |
| 284 | + "tags": [] |
| 285 | + }, |
| 286 | + "outputs": [], |
| 287 | + "source": [ |
| 288 | + "grid_plot[\"top-right-plot\"][\"rand-img\"].cmap.vmin = 0.5" |
| 289 | + ] |
| 290 | + }, |
| 291 | + { |
| 292 | + "cell_type": "code", |
| 293 | + "execution_count": 11, |
| 294 | + "id": "4f5f7fab-d7f2-4b6b-8f2d-c0745f9193cb", |
| 295 | + "metadata": { |
| 296 | + "tags": [] |
| 297 | + }, |
| 298 | + "outputs": [], |
| 299 | + "source": [ |
| 300 | + "sc.close()" |
| 301 | + ] |
| 302 | + }, |
| 303 | + { |
| 304 | + "cell_type": "code", |
| 305 | + "execution_count": null, |
| 306 | + "id": "204385ee-b164-4e74-8b03-e2f0013f9e47", |
| 307 | + "metadata": {}, |
| 308 | + "outputs": [], |
| 309 | + "source": [] |
| 310 | + } |
| 311 | + ], |
| 312 | + "metadata": { |
| 313 | + "kernelspec": { |
| 314 | + "display_name": "Python 3 (ipykernel)", |
| 315 | + "language": "python", |
| 316 | + "name": "python3" |
| 317 | + }, |
| 318 | + "language_info": { |
| 319 | + "codemirror_mode": { |
| 320 | + "name": "ipython", |
| 321 | + "version": 3 |
| 322 | + }, |
| 323 | + "file_extension": ".py", |
| 324 | + "mimetype": "text/x-python", |
| 325 | + "name": "python", |
| 326 | + "nbconvert_exporter": "python", |
| 327 | + "pygments_lexer": "ipython3", |
| 328 | + "version": "3.11.3" |
| 329 | + } |
| 330 | + }, |
| 331 | + "nbformat": 4, |
| 332 | + "nbformat_minor": 5 |
| 333 | +} |
0 commit comments