| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "markdown", |
| 5 |
"metadata": { |
| 6 |
"collapsed": true |
| 7 |
}, |
| 8 |
"source": [ |
| 9 |
"# Heatmap" |
| 10 |
] |
| 11 |
}, |
| 12 |
{ |
| 13 |
"cell_type": "markdown", |
| 14 |
"metadata": {}, |
| 15 |
"source": [ |
| 16 |
"The `HeatMap` mark represents a 2d matrix of values as a color image. It can be used to visualize a 2d function, or a grayscale image for instance.\n", |
| 17 |
"\n", |
| 18 |
"\n", |
| 19 |
"`HeatMap` is very similar to the `GridHeatMap`, but should be preferred for a greater number of points (starting at around 100x100), to avoid overloading the browser. `GridHeatMap` offers more control (interactions, selections), and is better suited for a smaller number of points." |
| 20 |
] |
| 21 |
}, |
| 22 |
{ |
| 23 |
"cell_type": "code", |
| 24 |
"execution_count": null, |
| 25 |
"metadata": {}, |
| 26 |
"outputs": [], |
| 27 |
"source": [ |
| 28 |
"import numpy as np\n", |
| 29 |
"from bqplot import (Figure, LinearScale,ColorScale, \n", |
| 30 |
" Color, Axis, HeatMap, ColorAxis)\n", |
| 31 |
"from ipywidgets import Layout" |
| 32 |
] |
| 33 |
}, |
| 34 |
{ |
| 35 |
"cell_type": "markdown", |
| 36 |
"metadata": {}, |
| 37 |
"source": [ |
| 38 |
"### Data Input\n", |
| 39 |
"\n", |
| 40 |
"- `x` is a 1d array, corresponding to the abscissas of the points (size N)\n", |
| 41 |
"- `y` is a 1d array, corresponding to the ordinates of the points (size M)\n", |
| 42 |
"- `color` is a 2d array, $\\text{color}_{ij}$ is the intensity of the point $(x_i, y_j)$ (size (N, M))\n", |
| 43 |
"\n", |
| 44 |
"Scales must be defined for each attribute:\n", |
| 45 |
"- a `LinearScale`, `LogScale` or `OrdinalScale` for `x` and `y`\n", |
| 46 |
"- a `ColorScale` for `color`" |
| 47 |
] |
| 48 |
}, |
| 49 |
{ |
| 50 |
"cell_type": "code", |
| 51 |
"execution_count": null, |
| 52 |
"metadata": {}, |
| 53 |
"outputs": [], |
| 54 |
"source": [ |
| 55 |
"x = np.linspace(-5, 5, 200)\n", |
| 56 |
"y = np.linspace(-5, 5, 200)\n", |
| 57 |
"X, Y = np.meshgrid(x, y)\n", |
| 58 |
"color = np.cos(X**2 + Y**2)" |
| 59 |
] |
| 60 |
}, |
| 61 |
{ |
| 62 |
"cell_type": "markdown", |
| 63 |
"metadata": {}, |
| 64 |
"source": [ |
| 65 |
"## Plotting a 2-dimensional function \n", |
| 66 |
"\n", |
| 67 |
"This is a visualization of the function $f(x, y) = \\text{cos}(x^2+y^2)$" |
| 68 |
] |
| 69 |
}, |
| 70 |
{ |
| 71 |
"cell_type": "code", |
| 72 |
"execution_count": null, |
| 73 |
"metadata": {}, |
| 74 |
"outputs": [], |
| 75 |
"source": [ |
| 76 |
"x_sc, y_sc, col_sc = LinearScale(), LinearScale(), ColorScale(scheme='RdYlBu')\n", |
| 77 |
"heat = HeatMap(x=x, y=y, color=color,\n", |
| 78 |
" scales={'x': x_sc, 'y': y_sc, 'color': col_sc})\n", |
| 79 |
"ax_x = Axis(scale=x_sc)\n", |
| 80 |
"ax_y = Axis(scale=y_sc, orientation='vertical')\n", |
| 81 |
"ax_c = ColorAxis(scale=col_sc)\n", |
| 82 |
"fig = Figure(marks=[heat], axes=[ax_x, ax_y, ax_c],\n", |
| 83 |
" title='Cosine',\n", |
| 84 |
" layout=Layout(width='650px', height='650px'),\n", |
| 85 |
" min_aspect_ratio=1, max_aspect_ratio=1, padding_y=0)\n", |
| 86 |
"fig" |
| 87 |
] |
| 88 |
}, |
| 89 |
{ |
| 90 |
"cell_type": "markdown", |
| 91 |
"metadata": {}, |
| 92 |
"source": [ |
| 93 |
"## Displaying an image\n", |
| 94 |
"\n", |
| 95 |
"The `HeatMap` can be used as is to display a 2d grayscale image, by feeding the matrix of pixel intensities to the `color` attribute" |
| 96 |
] |
| 97 |
}, |
| 98 |
{ |
| 99 |
"cell_type": "code", |
| 100 |
"execution_count": null, |
| 101 |
"metadata": {}, |
| 102 |
"outputs": [], |
| 103 |
"source": [ |
| 104 |
"from scipy.misc import ascent\n", |
| 105 |
"Z = ascent()\n", |
| 106 |
"Z = Z[::-1, :] \n", |
| 107 |
"aspect_ratio = Z.shape[1]/Z.shape[0]" |
| 108 |
] |
| 109 |
}, |
| 110 |
{ |
| 111 |
"cell_type": "code", |
| 112 |
"execution_count": null, |
| 113 |
"metadata": {}, |
| 114 |
"outputs": [], |
| 115 |
"source": [ |
| 116 |
"col_sc = ColorScale(scheme='Greys', reverse=True)\n", |
| 117 |
"scales = {'color': col_sc};\n", |
| 118 |
"ascent = HeatMap(color=Z, scales=scales)\n", |
| 119 |
"img = Figure(title='Ascent', marks=[ascent],\n", |
| 120 |
" layout=Layout(width='650px', height='650px'),\n", |
| 121 |
" min_aspect_ratio=aspect_ratio, max_aspect_ratio=aspect_ratio, padding_y=0)\n", |
| 122 |
"img" |
| 123 |
] |
| 124 |
} |
| 125 |
], |
| 126 |
"metadata": { |
| 127 |
"anaconda-cloud": {}, |
| 128 |
"kernelspec": { |
| 129 |
"display_name": "Python 3", |
| 130 |
"language": "python", |
| 131 |
"name": "python3" |
| 132 |
}, |
| 133 |
"language_info": { |
| 134 |
"codemirror_mode": { |
| 135 |
"name": "ipython", |
| 136 |
"version": 3 |
| 137 |
}, |
| 138 |
"file_extension": ".py", |
| 139 |
"mimetype": "text/x-python", |
| 140 |
"name": "python", |
| 141 |
"nbconvert_exporter": "python", |
| 142 |
"pygments_lexer": "ipython3", |
| 143 |
"version": "3.7.6" |
| 144 |
} |
| 145 |
}, |
| 146 |
"nbformat": 4, |
| 147 |
"nbformat_minor": 2 |
| 148 |
} |