Debian Codegraph @

bqplot/examples/Marks/Object Model/GridHeatMap.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "import numpy as np\n",
10 "from bqplot import (ColorScale, GridHeatMap, Axis, \n",
11 " Figure, OrdinalScale, LinearScale)"
12 ]
13 },
14 {
15 "cell_type": "markdown",
16 "metadata": {},
17 "source": [
18 "## Get Data"
19 ]
20 },
21 {
22 "cell_type": "code",
23 "execution_count": null,
24 "metadata": {},
25 "outputs": [],
26 "source": [
27 "np.random.seed(0)\n",
28 "data = np.random.randn(10, 10)"
29 ]
30 },
31 {
32 "cell_type": "markdown",
33 "metadata": {},
34 "source": [
35 "## Basic Heat map"
36 ]
37 },
38 {
39 "cell_type": "code",
40 "execution_count": null,
41 "metadata": {},
42 "outputs": [],
43 "source": [
44 "col_sc = ColorScale()\n",
45 "grid_map = GridHeatMap(color=data, scales={'color': col_sc})\n",
46 "\n",
47 "Figure(marks=[grid_map], padding_y=0.0)"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": null,
53 "metadata": {},
54 "outputs": [],
55 "source": [
56 "grid_map.display_format = '.2f'"
57 ]
58 },
59 {
60 "cell_type": "code",
61 "execution_count": null,
62 "metadata": {},
63 "outputs": [],
64 "source": [
65 "grid_map.font_style={'font-size': '12px', 'fill':'black', 'font-weight': 'bold'}"
66 ]
67 },
68 {
69 "cell_type": "code",
70 "execution_count": null,
71 "metadata": {},
72 "outputs": [],
73 "source": [
74 "grid_map.display_format = None"
75 ]
76 },
77 {
78 "cell_type": "markdown",
79 "metadata": {},
80 "source": [
81 "## Heat map with axes"
82 ]
83 },
84 {
85 "cell_type": "code",
86 "execution_count": null,
87 "metadata": {},
88 "outputs": [],
89 "source": [
90 "x_sc, y_sc, col_sc = OrdinalScale(), OrdinalScale(reverse=True), ColorScale()\n",
91 "\n",
92 "grid_map = GridHeatMap(color=data, scales={'column': x_sc, 'row': y_sc, 'color': col_sc})\n",
93 "ax_x, ax_y = Axis(scale=x_sc), Axis(scale=y_sc, orientation='vertical')\n",
94 "\n",
95 "Figure(marks=[grid_map], axes=[ax_x, ax_y], padding_y=0.0)"
96 ]
97 },
98 {
99 "cell_type": "markdown",
100 "metadata": {},
101 "source": [
102 "## Non Uniform Heat map"
103 ]
104 },
105 {
106 "cell_type": "code",
107 "execution_count": null,
108 "metadata": {},
109 "outputs": [],
110 "source": [
111 "x_sc, y_sc, col_sc = LinearScale(), LinearScale(reverse=True), ColorScale()\n",
112 "ax_x = Axis(scale=x_sc)\n",
113 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
114 "\n",
115 "## The data along the rows is not uniform. Hence the 5th row(from top) of the map\n",
116 "## is twice the height of the remaining rows.\n",
117 "row_data = np.arange(10)\n",
118 "row_data[5:] = np.arange(6, 11)\n",
119 "\n",
120 "column_data = np.arange(10, 20)\n",
121 "\n",
122 "grid_map = GridHeatMap(row=row_data, column=column_data, color=data,\n",
123 " scales={'row': y_sc, 'column': x_sc, 'color': col_sc})\n",
124 "Figure(marks=[grid_map], padding_y=0.0, axes=[ax_x, ax_y])"
125 ]
126 },
127 {
128 "cell_type": "code",
129 "execution_count": null,
130 "metadata": {
131 "scrolled": true
132 },
133 "outputs": [],
134 "source": [
135 "print(row_data.shape)\n",
136 "print(column_data.shape)\n",
137 "print(data.shape)"
138 ]
139 },
140 {
141 "cell_type": "markdown",
142 "metadata": {},
143 "source": [
144 "## Alignment of the data with respect to the grid"
145 ]
146 },
147 {
148 "cell_type": "markdown",
149 "metadata": {},
150 "source": [
151 "For a `N-by-N` matrix, `N+1` points along the row or the column are assumed to be end points."
152 ]
153 },
154 {
155 "cell_type": "code",
156 "execution_count": null,
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "x_sc, y_sc, col_sc = LinearScale(), LinearScale(reverse=True), ColorScale()\n",
161 "ax_x = Axis(scale=x_sc)\n",
162 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
163 "\n",
164 "row_data = np.arange(11)\n",
165 "column_data = np.arange(10, 21)\n",
166 "\n",
167 "grid_map = GridHeatMap(row=row_data, column=column_data, color=data,\n",
168 " scales={'row': y_sc, 'column': x_sc, 'color': col_sc})\n",
169 "Figure(marks=[grid_map], padding_y=0.0, axes=[ax_x, ax_y])"
170 ]
171 },
172 {
173 "cell_type": "markdown",
174 "metadata": {},
175 "source": [
176 "By default, for `N` points along any dimension, data aligns to the `start` of the rectangles in the grid. \n",
177 "The grid extends infinitely in the other direction. By default, the grid extends infinitely\n",
178 "towards the bottom and the right."
179 ]
180 },
181 {
182 "cell_type": "code",
183 "execution_count": null,
184 "metadata": {},
185 "outputs": [],
186 "source": [
187 "x_sc, y_sc, col_sc = LinearScale(), LinearScale(reverse=True, max=15), ColorScale()\n",
188 "ax_x = Axis(scale=x_sc)\n",
189 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
190 "\n",
191 "row_data = np.arange(10)\n",
192 "column_data = np.arange(10, 20)\n",
193 "\n",
194 "grid_map = GridHeatMap(row=row_data, column=column_data, color=data,\n",
195 " scales={'row': y_sc, 'column': x_sc, 'color': col_sc})\n",
196 "Figure(marks=[grid_map], padding_y=0.0, axes=[ax_x, ax_y])"
197 ]
198 },
199 {
200 "cell_type": "markdown",
201 "metadata": {},
202 "source": [
203 "By changing the `row_align` and `column_align` properties, the grid can extend in the opposite direction"
204 ]
205 },
206 {
207 "cell_type": "code",
208 "execution_count": null,
209 "metadata": {},
210 "outputs": [],
211 "source": [
212 "x_sc, y_sc, col_sc = LinearScale(), LinearScale(reverse=True, min=-5, max=15), ColorScale()\n",
213 "ax_x = Axis(scale=x_sc)\n",
214 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
215 "\n",
216 "row_data = np.arange(10)\n",
217 "column_data = np.arange(10, 20)\n",
218 "\n",
219 "grid_map = GridHeatMap(row=row_data, column=column_data, color=data,\n",
220 " scales={'row': y_sc, 'column': x_sc, 'color': col_sc},\n",
221 " row_align='end')\n",
222 "Figure(marks=[grid_map], padding_y=0.0, axes=[ax_x, ax_y])"
223 ]
224 },
225 {
226 "cell_type": "markdown",
227 "metadata": {},
228 "source": [
229 "For `N+1` points on any direction, the grid extends infinitely in both directions"
230 ]
231 },
232 {
233 "cell_type": "code",
234 "execution_count": null,
235 "metadata": {},
236 "outputs": [],
237 "source": [
238 "x_sc, y_sc, col_sc = LinearScale(), LinearScale(reverse=True, min=-5, max=15), ColorScale()\n",
239 "ax_x = Axis(scale=x_sc)\n",
240 "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
241 "\n",
242 "row_data = np.arange(9)\n",
243 "column_data = np.arange(10, 20)\n",
244 "\n",
245 "grid_map = GridHeatMap(row=row_data, column=column_data, color=data, \n",
246 " scales={'row': y_sc, 'column': x_sc, 'color': col_sc}, row_align='end')\n",
247 "Figure(marks=[grid_map], padding_y=0.0, axes=[ax_x, ax_y])"
248 ]
249 },
250 {
251 "cell_type": "markdown",
252 "metadata": {},
253 "source": [
254 "## Changing opacity and stroke"
255 ]
256 },
257 {
258 "cell_type": "code",
259 "execution_count": null,
260 "metadata": {},
261 "outputs": [],
262 "source": [
263 "col_sc = ColorScale()\n",
264 "\n",
265 "grid_map = GridHeatMap(color=data, scales={'color': col_sc}, opacity=0.3, stroke='white')\n",
266 "Figure(marks=[grid_map], padding_y=0.0)"
267 ]
268 },
269 {
270 "cell_type": "markdown",
271 "metadata": {},
272 "source": [
273 "## Selections on the grid map"
274 ]
275 },
276 {
277 "cell_type": "markdown",
278 "metadata": {},
279 "source": [
280 "Selection on the `GridHeatMap` works similar to excel. Clicking on a cell selects the cell, and deselects the previous selection. Using the `Ctrl` key allows multiple cells to be selected, while the `Shift` key selects the range from the last cell in the selection to the current cell."
281 ]
282 },
283 {
284 "cell_type": "code",
285 "execution_count": null,
286 "metadata": {},
287 "outputs": [],
288 "source": [
289 "data = np.random.randn(10, 10)\n",
290 "\n",
291 "col_sc = ColorScale()\n",
292 "grid_map = GridHeatMap(color=data, scales={'color': col_sc}, interactions={'click':'select'},\n",
293 " selected_style={'opacity': '1.0'}, unselected_style={'opacity': 0.4})\n",
294 "\n",
295 "Figure(marks=[grid_map], padding_y=0.0)"
296 ]
297 },
298 {
299 "cell_type": "markdown",
300 "metadata": {},
301 "source": [
302 "The `selected` trait of a `GridHeatMap` contains a list of lists, with each sub-list containing the row and column index of a selected cell."
303 ]
304 },
305 {
306 "cell_type": "code",
307 "execution_count": null,
308 "metadata": {
309 "scrolled": false
310 },
311 "outputs": [],
312 "source": [
313 "grid_map.selected"
314 ]
315 },
316 {
317 "cell_type": "markdown",
318 "metadata": {},
319 "source": [
320 "## Registering `on_element_click` event handler"
321 ]
322 },
323 {
324 "cell_type": "code",
325 "execution_count": null,
326 "metadata": {},
327 "outputs": [],
328 "source": [
329 "import numpy as np\n",
330 "from IPython.display import display\n",
331 "from bqplot import *\n",
332 "\n",
333 "np.random.seed(0)\n",
334 "data = np.random.randn(10, 10)\n",
335 "col_sc = ColorScale()\n",
336 "grid_map = GridHeatMap(color=data, scales={'color': col_sc}, \n",
337 " interactions={'click': 'select'},\n",
338 " selected_style={'stroke': 'blue', 'stroke-width': 3})\n",
339 "figure=Figure(marks=[grid_map], padding_y=0.0)\n",
340 "\n",
341 "from ipywidgets import Output\n",
342 "out = Output()\n",
343 "@out.capture()\n",
344 "def print_event(self, target):\n",
345 " print(target)\n",
346 " \n",
347 "# test\n",
348 "print_event(1, 'test output')\n",
349 "grid_map.on_element_click(print_event)\n",
350 "\n",
351 "display(figure)\n",
352 "display(out)"
353 ]
354 }
355 ],
356 "metadata": {
357 "anaconda-cloud": {},
358 "kernelspec": {
359 "display_name": "Python 3",
360 "language": "python",
361 "name": "python3"
362 },
363 "language_info": {
364 "codemirror_mode": {
365 "name": "ipython",
366 "version": 3
367 },
368 "file_extension": ".py",
369 "mimetype": "text/x-python",
370 "name": "python",
371 "nbconvert_exporter": "python",
372 "pygments_lexer": "ipython3",
373 "version": "3.7.6"
374 }
375 },
376 "nbformat": 4,
377 "nbformat_minor": 1
378 }