{ "cells": [ { "cell_type": "markdown", "id": "9d758db2-4423-4a02-80f5-2684c8282410", "metadata": {}, "source": [ "# Multi plot annotations\n", "\n", ":::{note}\n", "What you see in this notebook will depend on whether you've run this notebook before and written annotations to the `annotations.db` database! For reproducibility, the rest of the notebook will assume the `annotations.db` has been deleted (if it exists).\n", ":::\n", "\n", "## Setup\n", "The first thing we need to do is get and plot the data we want to annotate. In this example we will make two different plots: one curve and one image plot." ] }, { "cell_type": "code", "execution_count": null, "id": "519af352-eba9-41d2-a164-c4b6ae4945d3", "metadata": {}, "outputs": [], "source": [ "import holoviews as hv\n", "import numpy as np\n", "from holonote.annotate import Annotator\n", "\n", "hv.extension(\"bokeh\")" ] }, { "cell_type": "code", "execution_count": null, "id": "e91ab795-0b03-4b9a-a31c-fb20dbf8035d", "metadata": {}, "outputs": [], "source": [ "xs = np.arange(\"2005-02\", \"2005-04\", dtype=\"datetime64[D]\")\n", "ys = np.sin(np.linspace(0, 5 * np.pi, len(xs)))\n", "\n", "curve = hv.Curve((xs, ys), kdims=[\"TIME\"])\n", "curve" ] }, { "cell_type": "code", "execution_count": null, "id": "b8075ba5-8635-4e96-87e9-31d4e17be678", "metadata": {}, "outputs": [], "source": [ "xvals = np.linspace(-4, 0, 202)\n", "yvals = np.linspace(4, 0, 202)\n", "xs, ys = np.meshgrid(xvals, yvals)\n", "alpha, beta = 1, 0\n", "ab_data = np.sin(((ys / alpha) ** alpha + beta) * xs)\n", "\n", "image = hv.Image(ab_data, kdims=[\"A\", \"B\"]).opts(cmap=\"greens\")\n", "image" ] }, { "cell_type": "markdown", "id": "74f4a95b-2f7d-4f7b-83ef-b8f59e38fbee", "metadata": {}, "source": [ "We will now create the annotator with the specification consisting of the key dimensions (`kdims`) in both of the two plots: `TIME`, `A`, and `B`." ] }, { "cell_type": "code", "execution_count": null, "id": "d02d0a45-f044-40eb-ad99-1105826937ff", "metadata": {}, "outputs": [], "source": [ "spec = {\"TIME\": np.datetime64, \"A\": np.float64, \"B\": np.float64}\n", "annotator = Annotator(spec, fields=[\"description\"])" ] }, { "cell_type": "markdown", "id": "80b2b3fd-9c98-4735-a732-cbe2abfd9424", "metadata": {}, "source": [ "Then we can do the same as show in the basic example, and overlaying the annotator on top of both plots. " ] }, { "cell_type": "code", "execution_count": null, "id": "ddba311b-489b-4f04-ac2d-a9ad3db25cac", "metadata": {}, "outputs": [], "source": [ "annotator * curve + annotator * image" ] }, { "cell_type": "markdown", "id": "b8472fe4-7824-4992-a50d-9d35bb6187e8", "metadata": {}, "source": [ "## Basic operations on annotations\n", "\n", "Using the select tool, you can define a region of interest on both of the plots and run the following cell. For more information check out the [basic example](./Basics.ipynb).\n" ] }, { "cell_type": "code", "execution_count": null, "id": "a2f94aec-581f-4b1b-8e3c-033f9c679cd0", "metadata": {}, "outputs": [], "source": [ "annotator.add_annotation(description='My first annotation!')" ] }, { "cell_type": "markdown", "id": "2f1b1f18-af5a-4e18-ab3b-73502a9e3bf9", "metadata": {}, "source": [ "You can set the range of interest programmatically as well:" ] }, { "cell_type": "code", "execution_count": null, "id": "c1c09972-3e85-4b7e-90cc-3f3edde71c15", "metadata": {}, "outputs": [], "source": [ "annotator.set_regions(\n", " TIME=(np.datetime64(\"2005-02-13\"), np.datetime64(\"2005-02-16\")),\n", " A=(-0.25, 0.25),\n", " B=(-0.1, 0.1),\n", ")\n", "annotator.add_annotation(description='A programmatically defined annotation')" ] }, { "cell_type": "markdown", "id": "4bd47182-e15e-4334-96f5-3c2ace10590b", "metadata": {}, "source": [ "To persist these annotations, we call the `.commit()` method:" ] }, { "cell_type": "code", "execution_count": null, "id": "b7688ef7-d815-4bee-a31a-ae2e5343f3cc", "metadata": {}, "outputs": [], "source": [ "annotator.commit()" ] }, { "cell_type": "markdown", "id": "5f1e18ec-4f80-4de0-b796-e069921e8d04", "metadata": {}, "source": [ "#### Simple selection of annotations\n", "\n", "Click on a range region in the plot above and run the following cell to see its UUID:" ] }, { "cell_type": "code", "execution_count": null, "id": "0b02700b-f2bf-4a9b-a93b-227b72bf6c25", "metadata": {}, "outputs": [], "source": [ "annotator.selected_index" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }