From e5f2e60804fcd9bfcb656824dfcd6bc95f36bc1e Mon Sep 17 00:00:00 2001 From: kushalkolar Date: Thu, 6 Jul 2023 04:13:35 -0400 Subject: [PATCH] real images in quickstart guide --- docs/source/quickstart.ipynb | 369 +++++++++++++++++++++++++++++++---- 1 file changed, 327 insertions(+), 42 deletions(-) diff --git a/docs/source/quickstart.ipynb b/docs/source/quickstart.ipynb index 66543c063..aebe04b25 100644 --- a/docs/source/quickstart.ipynb +++ b/docs/source/quickstart.ipynb @@ -9,37 +9,57 @@ "source": [ "# Quick Start Guide 🚀\n", "\n", - "This notebook goes the basic components of the `fastplotlib` API, image, image updates, line plots, and scatter plots.\n", + "This notebook goes through the basic components of the `fastplotlib` API, images, image updates, line plots, scatter plots, and grid plots.\n", "\n", "**NOTE: This quick start guide in the docs is NOT interactive. Download the examples from the repo and try them on your own computer. You can run the desktop examples directly if you have `glfw` installed, or try the notebook demos:** https://github.com/kushalkolar/fastplotlib/tree/master/examples\n", "\n", "It will not be possible to have live demos on the docs until someone can figure out how to get [pygfx](https://github.com/pygfx/pygfx) to work with `wgpu` in the browser, perhaps through [pyodide](https://github.com/pyodide/pyodide) or something :D." ] }, + { + "cell_type": "markdown", + "id": "5d21c330-89cd-49ab-9069-4e3652d4286b", + "metadata": {}, + "source": [ + "**The example images are from `imageio` so you will need to install it for this example notebook. But `imageio` is not required to use `fasptlotlib`**" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "fb57c3d3-f20d-4d88-9e7a-04b9309bc637", + "id": "07f064bb-025a-4794-9b05-243810edaf60", "metadata": { "tags": [] }, "outputs": [], "source": [ - "import fastplotlib as fpl\n", - "from ipywidgets import VBox, HBox, IntSlider\n", - "import numpy as np" + "!pip install imageio" ] }, { "cell_type": "code", "execution_count": null, - "id": "2d663646-3d3b-4f5b-a083-a5daca65cb4f", + "id": "5f842366-bd39-47de-ad00-723b2be707e4", "metadata": { "tags": [] }, "outputs": [], "source": [ - "import os" + "import imageio.v3 as iio" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb57c3d3-f20d-4d88-9e7a-04b9309bc637", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import fastplotlib as fpl\n", + "from ipywidgets import VBox, HBox, IntSlider\n", + "import numpy as np" ] }, { @@ -62,11 +82,11 @@ "# create a `Plot` instance\n", "plot = fpl.Plot()\n", "\n", - "# make some random 2D image data\n", - "data = np.random.rand(512, 512)\n", + "# get a grayscale image\n", + "data = iio.imread(\"imageio:camera.png\")\n", "\n", "# plot the image data\n", - "image_graphic = plot.add_image(data=data, name=\"random-image\")\n", + "image_graphic = plot.add_image(data=data, name=\"sample-image\")\n", "\n", "# show the plot\n", "plot.show()" @@ -77,21 +97,55 @@ "id": "be5b408f-dd91-4e36-807a-8c22c8d7d216", "metadata": {}, "source": [ - "In live notebooks or desktop applications, you can use the handle on the bottom right corner of the _canvas_ to resize it. You can also pan and zoom using your mouse!" + "**In live notebooks or desktop applications, you can use the handle on the bottom right corner of the _canvas_ to resize it. You can also pan and zoom using your mouse!**\n", + "\n", + "By default the origin is on the bottom left, you can click the flip button to flip the y-axis, or use `plot.camera.world.scale_y *= -1`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9b4e977e-2a7d-4e2b-aee4-cfc36767b3c6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot.camera.world.scale_y *= -1" + ] + }, + { + "cell_type": "markdown", + "id": "9ba07ec1-a0cb-4461-87c6-c7b64d4a882b", + "metadata": {}, + "source": [ + "This is how you can take a snapshot of the canvas. Snapshots are shown throughout this doc page for the purposes of documentation, they are NOT necessary for real interactive usage. Download the notebooks to run live demos." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b64ba135-e753-43a9-ad1f-adcc7310792d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot.canvas.snapshot()" ] }, { "cell_type": "markdown", - "id": "7c3b637c-a26b-416e-936c-705275852a8a", + "id": "ac5f5e75-9aa4-441f-9a41-66c22cd53de8", "metadata": {}, "source": [ - "Changing graphic \"features\"" + "Changing graphic **\"features\"**" ] }, { "cell_type": "code", "execution_count": null, - "id": "de816c88-1c4a-4071-8a5e-c46c93671ef5", + "id": "d3541d1d-0819-450e-814c-588ffc8e7ed5", "metadata": { "tags": [] }, @@ -100,18 +154,59 @@ "image_graphic.cmap = \"viridis\"" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "ab544719-9187-45bd-8127-aac79eea30e4", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot.canvas.snapshot()" + ] + }, { "cell_type": "markdown", - "id": "ecc72f23-22ea-4bd1-b9a0-fd4e14baa79f", + "id": "9693cf94-11e9-46a6-a5b7-b0fbed42ad81", "metadata": {}, "source": [ - "This is how you can take a snapshot of the canvas. Snapshots are shown throughout this doc page for the purposes of documentation, they are NOT necessary for real interactive usage. Download the notebooks to run live demos." + "### Slicing data\n", + "\n", + "**Most features, such as `data` support slicing!**\n", + "\n", + "Out image data is of shape [n_rows, n_cols]" ] }, { "cell_type": "code", "execution_count": null, - "id": "ebc87904-f705-46f0-8f94-fc3b1c6c8e30", + "id": "330a47b5-50b1-4e6a-b8ab-d55d92af2042", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "image_graphic.data().shape" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "601f46d9-7f32-4a43-9090-4674218800ea", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "image_graphic.data[::15, :] = 1\n", + "image_graphic.data[:, ::15] = 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3443948f-9ac9-484a-a4bf-3a06c1ce5658", "metadata": { "tags": [] }, @@ -122,28 +217,28 @@ }, { "cell_type": "markdown", - "id": "de0653cf-937e-4d0f-965d-296fccaac53e", + "id": "53125b3b-3ce2-43c5-b2e3-7cd37cec7d7d", "metadata": {}, "source": [ - "Setting image data" + "**Fancy indexing**" ] }, { "cell_type": "code", "execution_count": null, - "id": "09350854-5058-4574-a01d-84d00e276c57", + "id": "7344cbbe-40c3-4d9e-ae75-7abe3ddaeeeb", "metadata": { "tags": [] }, "outputs": [], "source": [ - "image_graphic.data = 0" + "image_graphic.data[data > 175] = 255" ] }, { "cell_type": "code", "execution_count": null, - "id": "9bcc3943-ea40-4905-a2a2-29e2620f00c8", + "id": "ef113d79-5d86-4be0-868e-30f82f8ab528", "metadata": { "tags": [] }, @@ -154,29 +249,29 @@ }, { "cell_type": "markdown", - "id": "05034a44-f207-45a0-9b5e-3ba7cc118107", + "id": "4df5296e-2a18-403f-82f1-acb8eaf280e3", "metadata": {}, "source": [ - "Setting image data with slicing" + "Adjust vmin vmax" ] }, { "cell_type": "code", "execution_count": null, - "id": "83b2db1b-2783-4e89-bcf3-66bb6e09e18a", + "id": "28af88d1-0518-47a4-ab73-431d6aaf9cb8", "metadata": { "tags": [] }, "outputs": [], "source": [ - "image_graphic.data[::15, :] = 1\n", - "image_graphic.data[:, ::15] = 1" + "image_graphic.cmap.vmin = 50\n", + "image_graphic.cmap.vmax = 150" ] }, { "cell_type": "code", "execution_count": null, - "id": "d400b00b-bdf0-4383-974f-9cccd4cd48b6", + "id": "e3dfb827-c812-447d-b413-dc15653160b1", "metadata": { "tags": [] }, @@ -187,28 +282,64 @@ }, { "cell_type": "markdown", - "id": "4abfe97e-8aa6-42c0-8b23-797153a885e3", + "id": "19a1b56b-fdca-40c5-91c9-3c9486fd8a21", "metadata": {}, "source": [ - "Setting image data back to random" + "**Set the entire data array again**\n", + "\n", + "Note: The shape of the new data array must match the current data shown in the Graphic." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4dc3d0e4-b128-42cd-a53e-76846fc9b8a8", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "new_data = iio.imread(\"imageio:astronaut.png\")\n", + "new_data.shape" + ] + }, + { + "cell_type": "markdown", + "id": "3bd06068-fe3f-404d-ba4a-a72a2105904f", + "metadata": {}, + "source": [ + "This is an RGB image, convert to grayscale to maintain the shape of (512, 512)" ] }, { "cell_type": "code", "execution_count": null, - "id": "3e298c1c-7551-4401-ade0-b9af7d2bbe23", + "id": "150047a6-a6ac-442d-a468-3e0c224a2b7e", "metadata": { "tags": [] }, "outputs": [], "source": [ - "image_graphic.data = np.random.rand(512, 512)" + "gray = new_data.dot([0.3, 0.6, 0.1])\n", + "gray.shape" ] }, { "cell_type": "code", "execution_count": null, - "id": "49d44536-b36c-47be-9c09-46a81a2c8607", + "id": "bf24576b-d336-4754-9992-9649ccaa4d1e", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "image_graphic.data = gray" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67d810c2-4020-4769-a5ba-0d4a972ee243", "metadata": { "tags": [] }, @@ -219,16 +350,88 @@ }, { "cell_type": "markdown", - "id": "67b92ffd-40cc-43fe-9df9-0e0d94763d8e", + "id": "2fe82654-e554-4be6-92a0-ecdee0ef8519", "metadata": {}, "source": [ - "Plots are indexable and give you their graphics by name" + "reset vmin vmax" ] }, { "cell_type": "code", "execution_count": null, - "id": "e6ba689c-ff4a-44ef-9663-f2c8755072c4", + "id": "0be6e4bb-cf9a-4155-9f6a-8106e66e6132", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "image_graphic.cmap.reset_vmin_vmax()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "bd51936c-ad80-4b33-b855-23565265a430", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot.canvas.snapshot()" + ] + }, + { + "cell_type": "markdown", + "id": "a6c1f3fb-a3a7-4175-bd8d-bb3203740771", + "metadata": {}, + "source": [ + "### Indexing plots" + ] + }, + { + "cell_type": "markdown", + "id": "3fc38694-aca6-4f56-97ac-3435059a6be7", + "metadata": {}, + "source": [ + "**Plots are indexable and give you their graphics by name**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8a547138-0f7d-470b-9925-8df479c3979e", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5551861f-9860-4515-8222-2f1c6d6a3220", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot[\"sample-image\"]" + ] + }, + { + "cell_type": "markdown", + "id": "0c29b36e-0eb4-4bb3-a8db-add58c303ee8", + "metadata": {}, + "source": [ + "**You can also use numerical indexing on `plot.graphics`**" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ce6adbb0-078a-4e74-b189-58f860ee5df5", "metadata": { "tags": [] }, @@ -240,18 +443,18 @@ { "cell_type": "code", "execution_count": null, - "id": "5b18f4e3-e13b-46d5-af1f-285c5a7fdc12", + "id": "119bd6af-c486-4378-bc23-79b1759aa3a4", "metadata": { "tags": [] }, "outputs": [], "source": [ - "plot[\"random-image\"]" + "plot.graphics[0]" ] }, { "cell_type": "markdown", - "id": "4316a8b5-5f33-427a-8f52-b101d1daab67", + "id": "6b8e3f0d-56f8-447f-bf26-b52629d06e95", "metadata": {}, "source": [ "The `Graphic` instance is also returned when you call `plot.add_`." @@ -260,7 +463,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2b5c1321-1fd4-44bc-9433-7439ad3e22cf", + "id": "967c0cbd-287c-4d99-9891-9baf18f7b56a", "metadata": { "tags": [] }, @@ -272,13 +475,95 @@ { "cell_type": "code", "execution_count": null, - "id": "b12bf75e-4e93-4930-9146-e96324fdf3f6", + "id": "5da72e26-3536-47b8-839c-53452dd94f7a", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "image_graphic == plot[\"sample-image\"]" + ] + }, + { + "cell_type": "markdown", + "id": "2b5ee18b-e61b-415d-902a-688b1c9c03b8", + "metadata": {}, + "source": [ + "### RGB images\n", + "\n", + "`cmap` arguments are ignored for rgb images, but vmin vmax still works" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1f7143ec-8ee1-47d2-b017-d0a8efc69fc6", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot_rgb = fpl.Plot()\n", + "\n", + "plot_rgb.add_image(new_data, name=\"rgb-image\")\n", + "\n", + "plot_rgb.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7e4b5a30-4293-4ae3-87dc-06a1355bb2c7", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot_rgb.camera.world.scale_y *= -1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a47b1eaf-3638-470a-88a5-0026c81d7e2b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot_rgb.canvas.snapshot()" + ] + }, + { + "cell_type": "markdown", + "id": "4848a929-4f3b-46d7-921b-ebfe8de0ebb5", + "metadata": {}, + "source": [ + "vmin and vmax are still applicable to rgb images" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ffe50132-8dd0-433c-b9c6-9ead8c3d48de", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "plot_rgb[\"rgb-image\"].cmap.vmin = 100" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "161468ba-b836-4021-8d11-8dfc140b94eb", "metadata": { "tags": [] }, "outputs": [], "source": [ - "image_graphic == plot[\"random-image\"]" + "plot_rgb.canvas.snapshot()" ] }, { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy