Debian Codegraph @

bqplot/examples/Tutorials/Brush Interval Selector.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Linking Plots Using Brush Interval Selector"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "Details on how to use the brush interval selector can be found in [this](../Interactions/Selectors.ipynb#brushselectors) notebook.\n",
15 "\n",
16 "Brush interval selectors can be used where *continuous updates* are **not** desirable (for example, in callbacks performing slower computations)\n",
17 "\n",
18 "The boolean trait `brushing` can be used to control *continuous updates* in the `BrushSelector`. `brushing` will be set to `False` when the interval selector is not brushing. We can register callbacks by listening to the `brushing` trait of the brush selector. We can check the value of `brushing` trait in the callback and perform updates *only* at the end of brushing.\n",
19 "\n",
20 "Let's now look at an example of linking a time series plot to a scatter plot using a `BrushIntervalSelector`"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": null,
26 "metadata": {},
27 "outputs": [],
28 "source": [
29 "import numpy as np"
30 ]
31 },
32 {
33 "cell_type": "code",
34 "execution_count": null,
35 "metadata": {},
36 "outputs": [],
37 "source": [
38 "from ipywidgets import Layout, HTML, VBox\n",
39 "import bqplot.pyplot as plt"
40 ]
41 },
42 {
43 "cell_type": "markdown",
44 "metadata": {},
45 "source": [
46 "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"
47 ]
48 },
49 {
50 "cell_type": "code",
51 "execution_count": null,
52 "metadata": {},
53 "outputs": [],
54 "source": [
55 "from bqplot.interacts import BrushIntervalSelector\n",
56 "\n",
57 "y1, y2 = np.random.randn(2, 200).cumsum(axis=1) # two simple random walks\n",
58 "\n",
59 "fig_layout = Layout(width='900px', height='500px')\n",
60 "time_series_fig = plt.figure(layout=fig_layout)\n",
61 "line = plt.plot([y1, y2])\n",
62 "\n",
63 "# create a fast interval selector by passing in the X scale and the line mark on which the selector operates\n",
64 "intsel = BrushIntervalSelector(marks=[line], scale=line.scales['x'])\n",
65 "time_series_fig.interaction = intsel # set the interval selector on the figure"
66 ]
67 },
68 {
69 "cell_type": "markdown",
70 "metadata": {},
71 "source": [
72 "Let's now create a scatter plot of the two time series and stack it below the time series plot using a `VBox`"
73 ]
74 },
75 {
76 "cell_type": "code",
77 "execution_count": null,
78 "metadata": {},
79 "outputs": [],
80 "source": [
81 "scat_fig = plt.figure(layout=fig_layout, \n",
82 " animation_duration=750,\n",
83 " title='Scatter of time series slice selected by the interval selector')\n",
84 "# set the x and y attributes to the y values of line.y\n",
85 "scat = plt.scatter(*line.y, colors=['red'], stroke='black') "
86 ]
87 },
88 {
89 "cell_type": "code",
90 "execution_count": null,
91 "metadata": {},
92 "outputs": [],
93 "source": [
94 "# define a callback for the interval selector\n",
95 "def update_scatter(*args):\n",
96 " brushing = intsel.brushing\n",
97 " \n",
98 " # update scatter *only* when the interval selector\n",
99 " # is not brushing to prevent continuous updates\n",
100 " if not brushing:\n",
101 " # interval selector is active\n",
102 " if line.selected is not None:\n",
103 " # get the start and end indices of the interval\n",
104 " start_ix, end_ix = line.selected[0], line.selected[-1]\n",
105 " else: # interval selector is *not* active\n",
106 " start_ix, end_ix = 0, -1\n",
107 " \n",
108 " #update the x and y attributes of the scatter by slicing line.y\n",
109 " with scat.hold_sync():\n",
110 " scat.x, scat.y = line.y[:, start_ix:end_ix]\n",
111 "\n",
112 "# register the callback with brushing trait of interval selector\n",
113 "intsel.observe(update_scatter, 'brushing')"
114 ]
115 },
116 {
117 "cell_type": "code",
118 "execution_count": null,
119 "metadata": {},
120 "outputs": [],
121 "source": [
122 "help_label = HTML('<div style=\"color: blue; font-size: 16px; margin:20px 0px 0px 50px\">\\\n",
123 " Brush on the time series plot to activate the interval selector</div>')\n",
124 "VBox([help_label, time_series_fig, scat_fig])"
125 ]
126 }
127 ],
128 "metadata": {
129 "kernelspec": {
130 "display_name": "Python 3",
131 "language": "python",
132 "name": "python3"
133 },
134 "language_info": {
135 "codemirror_mode": {
136 "name": "ipython",
137 "version": 3
138 },
139 "file_extension": ".py",
140 "mimetype": "text/x-python",
141 "name": "python",
142 "nbconvert_exporter": "python",
143 "pygments_lexer": "ipython3",
144 "version": "3.7.6"
145 }
146 },
147 "nbformat": 4,
148 "nbformat_minor": 1
149 }