Debian Codegraph @

bqplot/examples/Marks/Pyplot/HeatMap.ipynb

raw
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 ipywidgets import Layout\n",
30 "import bqplot.pyplot as plt\n",
31 "from bqplot import ColorScale"
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 "fig = plt.figure(title='Cosine',\n",
77 " layout=Layout(width='650px', height='650px'),\n",
78 " min_aspect_ratio=1, max_aspect_ratio=1, padding_y=0)\n",
79 "heatmap = plt.heatmap(color, x=x, y=y)\n",
80 "fig"
81 ]
82 },
83 {
84 "cell_type": "markdown",
85 "metadata": {},
86 "source": [
87 "## Displaying an image\n",
88 "\n",
89 "The `HeatMap` can be used as is to display a 2d grayscale image, by feeding the matrix of pixel intensities to the `color` attribute"
90 ]
91 },
92 {
93 "cell_type": "code",
94 "execution_count": null,
95 "metadata": {},
96 "outputs": [],
97 "source": [
98 "from scipy.misc import ascent\n",
99 "Z = ascent()\n",
100 "Z = Z[::-1, :]\n",
101 "aspect_ratio = Z.shape[1] / Z.shape[0]"
102 ]
103 },
104 {
105 "cell_type": "code",
106 "execution_count": null,
107 "metadata": {},
108 "outputs": [],
109 "source": [
110 "img = plt.figure(title='Ascent', layout=Layout(width='650px', height='650px'),\n",
111 " min_aspect_ratio=aspect_ratio, \n",
112 " max_aspect_ratio=aspect_ratio, padding_y=0)\n",
113 "plt.scales(scales={'color': ColorScale(scheme='Greys', reverse=True)})\n",
114 "axes_options = {'x': {'visible': False}, 'y': {'visible': False}, 'color': {'visible': False}}\n",
115 "ascent = plt.heatmap(Z, axes_options=axes_options)\n",
116 "img"
117 ]
118 }
119 ],
120 "metadata": {
121 "anaconda-cloud": {},
122 "kernelspec": {
123 "display_name": "Python 3",
124 "language": "python",
125 "name": "python3"
126 },
127 "language_info": {
128 "codemirror_mode": {
129 "name": "ipython",
130 "version": 3
131 },
132 "file_extension": ".py",
133 "mimetype": "text/x-python",
134 "name": "python",
135 "nbconvert_exporter": "python",
136 "pygments_lexer": "ipython3",
137 "version": "3.7.6"
138 }
139 },
140 "nbformat": 4,
141 "nbformat_minor": 2
142 }