{ "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 ipywidgets import Layout\n", "import bqplot.pyplot as plt\n", "from bqplot import ColorScale" ] }, { "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": [ "fig = plt.figure(title='Cosine',\n", " layout=Layout(width='650px', height='650px'),\n", " min_aspect_ratio=1, max_aspect_ratio=1, padding_y=0)\n", "heatmap = plt.heatmap(color, x=x, y=y)\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": [ "img = plt.figure(title='Ascent', layout=Layout(width='650px', height='650px'),\n", " min_aspect_ratio=aspect_ratio, \n", " max_aspect_ratio=aspect_ratio, padding_y=0)\n", "plt.scales(scales={'color': ColorScale(scheme='Greys', reverse=True)})\n", "axes_options = {'x': {'visible': False}, 'y': {'visible': False}, 'color': {'visible': False}}\n", "ascent = plt.heatmap(Z, axes_options=axes_options)\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 }