Debian Codegraph @

bqplot/examples/Tutorials/Fast Interval Selector.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Linking Plots Using Fast Interval Selector"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "Details on how to use the fast interval selector can be found in [this](../Interactions/Selectors.ipynb#fastintervalselector) notebook. In this tutorial notebook, we will look at linking plots using the fast interval selector. Interval selectors are ideally suited for time series analysis where we want to perform statistical computations on different time periods.\n",
15 "\n",
16 "When creating an interval selector we need pass in the x scale. We can additionally pass in the marks on which the interval selector operates. When the interval selector moves, it updates the following traits (on which callbacks can be registered)\n",
17 "* `selected` trait of the interval selector object itself. This trait contains the start and end of the x axis obtained from the x scale. Note that these values may not be present in the `x` attribute of the marks!\n",
18 "* `selected` trait of the marks on which the selector operates. This trait contains *all* the *indices* of the line.x which are spanned by the interval selector.\n",
19 "\n",
20 "The recipe for linking plots is as follows:\n",
21 "\n",
22 "1. Create an interval selector object by passing in the X-scale and marks on which the selector should operate on (optional)\n",
23 "2. Pass it to the figure through the `interaction` argument\n",
24 "3. Register a callback (which potentially updates other plots) on either the `selected` attribute of the interval selector object or `selected` attribute of the marks objects in the figure\n",
25 "\n",
26 "<br>`FastIntervalSelector` updates the `selected` trait **continuously**. Therefore, registering computationally intensive callbacks is *not* recommended with `FastIntervalSelector`\n",
27 "\n",
28 "Let's now look at an example of linking a time series plot to a scatter plot using a `FastIntervalSelector`"
29 ]
30 },
31 {
32 "cell_type": "code",
33 "execution_count": null,
34 "metadata": {},
35 "outputs": [],
36 "source": [
37 "import numpy as np"
38 ]
39 },
40 {
41 "cell_type": "code",
42 "execution_count": null,
43 "metadata": {},
44 "outputs": [],
45 "source": [
46 "from ipywidgets import Layout, HTML, VBox\n",
47 "import bqplot.pyplot as plt"
48 ]
49 },
50 {
51 "cell_type": "markdown",
52 "metadata": {},
53 "source": [
54 "Let's set up an interval selector on a figure containing two time series plots. The interval selector can be activated by clicking on the figure"
55 ]
56 },
57 {
58 "cell_type": "code",
59 "execution_count": null,
60 "metadata": {},
61 "outputs": [],
62 "source": [
63 "from bqplot.interacts import FastIntervalSelector\n",
64 "\n",
65 "y1, y2 = np.random.randn(2, 200).cumsum(axis=1) # two simple random walks\n",
66 "\n",
67 "fig_layout = Layout(width='900px', height='500px')\n",
68 "time_series_fig = plt.figure(layout=fig_layout)\n",
69 "line = plt.plot([y1, y2])\n",
70 "\n",
71 "# create a fast interval selector by passing in the X scale and the line mark on which the selector operates\n",
72 "intsel = FastIntervalSelector(marks=[line], scale=line.scales['x'])\n",
73 "time_series_fig.interaction = intsel # set the interval selector on the figure"
74 ]
75 },
76 {
77 "cell_type": "markdown",
78 "metadata": {},
79 "source": [
80 "Let's now create a scatter plot of the two time series and stack it below the time series plot using a `VBox`"
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": null,
86 "metadata": {},
87 "outputs": [],
88 "source": [
89 "scat_fig = plt.figure(layout=fig_layout, title='Scatter of time series slice selected by the interval selector')\n",
90 "# set the x and y attributes to the y values of line.y\n",
91 "scat = plt.scatter(*line.y, colors=['red'], stroke='black') "
92 ]
93 },
94 {
95 "cell_type": "code",
96 "execution_count": null,
97 "metadata": {},
98 "outputs": [],
99 "source": [
100 "# define a callback for the interval selector\n",
101 "def update_scatter(*args):\n",
102 " # get the start and end indices of the interval\n",
103 " start_ix, end_ix = line.selected[0], line.selected[-1]\n",
104 " \n",
105 " #update the x and y attributes of the scatter by slicing line.y\n",
106 " with scat.hold_sync():\n",
107 " scat.x, scat.y = line.y[:, start_ix:end_ix]\n",
108 "\n",
109 "# register the callback with line.selected trait\n",
110 "line.observe(update_scatter, 'selected')"
111 ]
112 },
113 {
114 "cell_type": "code",
115 "execution_count": null,
116 "metadata": {},
117 "outputs": [],
118 "source": [
119 "help_label = HTML('<div style=\"color: blue; font-size: 16px; margin:20px 0px 0px 50px\">\\\n",
120 " Click on the time series plot to activate the interval selector</div>')\n",
121 "VBox([help_label, time_series_fig, scat_fig])"
122 ]
123 }
124 ],
125 "metadata": {
126 "kernelspec": {
127 "display_name": "Python 3",
128 "language": "python",
129 "name": "python3"
130 },
131 "language_info": {
132 "codemirror_mode": {
133 "name": "ipython",
134 "version": 3
135 },
136 "file_extension": ".py",
137 "mimetype": "text/x-python",
138 "name": "python",
139 "nbconvert_exporter": "python",
140 "pygments_lexer": "ipython3",
141 "version": "3.7.6"
142 }
143 },
144 "nbformat": 4,
145 "nbformat_minor": 1
146 }