{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Heatmap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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", "\n", "\n", "`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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from bqplot import (Figure, LinearScale,ColorScale, \n", " Color, Axis, HeatMap, ColorAxis)\n", "from ipywidgets import Layout" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data Input\n", "\n", "- `x` is a 1d array, corresponding to the abscissas of the points (size N)\n", "- `y` is a 1d array, corresponding to the ordinates of the points (size M)\n", "- `color` is a 2d array, $\\text{color}_{ij}$ is the intensity of the point $(x_i, y_j)$ (size (N, M))\n", "\n", "Scales must be defined for each attribute:\n", "- a `LinearScale`, `LogScale` or `OrdinalScale` for `x` and `y`\n", "- a `ColorScale` for `color`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.linspace(-5, 5, 200)\n", "y = np.linspace(-5, 5, 200)\n", "X, Y = np.meshgrid(x, y)\n", "color = np.cos(X**2 + Y**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting a 2-dimensional function \n", "\n", "This is a visualization of the function $f(x, y) = \\text{cos}(x^2+y^2)$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x_sc, y_sc, col_sc = LinearScale(), LinearScale(), ColorScale(scheme='RdYlBu')\n", "heat = HeatMap(x=x, y=y, color=color,\n", " scales={'x': x_sc, 'y': y_sc, 'color': col_sc})\n", "ax_x = Axis(scale=x_sc)\n", "ax_y = Axis(scale=y_sc, orientation='vertical')\n", "ax_c = ColorAxis(scale=col_sc)\n", "fig = Figure(marks=[heat], axes=[ax_x, ax_y, ax_c],\n", " title='Cosine',\n", " layout=Layout(width='650px', height='650px'),\n", " min_aspect_ratio=1, max_aspect_ratio=1, padding_y=0)\n", "fig" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Displaying an image\n", "\n", "The `HeatMap` can be used as is to display a 2d grayscale image, by feeding the matrix of pixel intensities to the `color` attribute" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from scipy.misc import ascent\n", "Z = ascent()\n", "Z = Z[::-1, :] \n", "aspect_ratio = Z.shape[1]/Z.shape[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "col_sc = ColorScale(scheme='Greys', reverse=True)\n", "scales = {'color': col_sc};\n", "ascent = HeatMap(color=Z, scales=scales)\n", "img = Figure(title='Ascent', marks=[ascent],\n", " layout=Layout(width='650px', height='650px'),\n", " min_aspect_ratio=aspect_ratio, max_aspect_ratio=aspect_ratio, padding_y=0)\n", "img" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }