Debian Codegraph @

bqplot/examples/Marks/Pyplot/GridHeatMap.ipynb

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