{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#

     Scatter plots in Lightning" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from lightning import Lightning\n", "\n", "from numpy import random, asarray, sqrt, arctan2, pi, clip\n", "from seaborn import color_palette\n", "from sklearn import datasets\n", "from colorsys import hsv_to_rgb" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Connect to server" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
Lightning initialized
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Connected to server at http://public.lightning-viz.org\n" ] }, { "data": { "application/javascript": [ "(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error(\"Cannot find module '\"+o+\"'\")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "lgn = Lightning(ipython=True, host='http://public.lightning-viz.org')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Random points with default styling" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 100\n", "x = random.randn(n)\n", "y = random.randn(n)\n", "lgn.scatter(x, y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Random big purple points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Style options like `color` and `size` can be passed a single value, which affects all points" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 100\n", "x = random.randn(n)\n", "y = random.randn(n)\n", "c = [145,117,240]\n", "s = 18\n", "lgn.scatter(x, y, color=c, size=s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Random points with all styling options" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Style options can also be passed as lists with one value per point, either scalars (for `alpha` and `size`) or arrays (for `color`). \n", "
\n", "In this example we also use seaborn's excellent `color_palette` tool to select our colors." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 100\n", "x = random.randn(n)\n", "y = random.randn(n)\n", "c = [asarray(color_palette('Blues', 100)[random.choice(range(100))])*255 for i in range(n)]\n", "a = random.rand(n)\n", "s = random.rand(n)*15+8\n", "lgn.scatter(x, y, color=c, alpha=a, size=s)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Clustered points with group labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of specifying colors directly as rgb, you can specify labels (or group assignments).\n", "
\n", "Here we use `scikitlearn` to generate clusters and then color according to cluster label." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d, g = datasets.make_blobs(n_features=2, n_samples=200, centers=5, cluster_std=2.0, random_state=100)\n", "x = d[:, 0]\n", "y = d[:, 1]\n", "lgn.scatter(x, y, group=g, alpha=0.8, size=12)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Color by value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can color points by a numerical value by passing that value, and a Colorbrewer colormap." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 200\n", "x = random.randn(n)\n", "y = random.randn(n)\n", "v = random.rand(n)\n", "lgn.scatter(x, y, values=v, alpha=0.6, colormap='YlOrRd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Fun with colors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use geometry to set colors based on point position and get a pretty result." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 200\n", "x = random.randn(n)\n", "y = random.randn(n)\n", "r = map(lambda (x, y): sqrt(x ** 2 + y ** 2), zip(x,y))\n", "t = map(lambda (x, y): arctan2(x, y), zip(x,y))\n", "c = map(lambda (r, t): asarray(hsv_to_rgb(t / (2 * pi), r, 0.9))*255, zip(r, t))\n", "s = asarray(r) * 10 + 2\n", "lgn.scatter(x, y, color=c, size=s, alpha=0.6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Axis labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can add axis labels by providing extra arguments" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = random.randn(100)\n", "y = random.randn(100)\n", "\n", "lgn.scatter(x, y, xaxis='my axis label 1', yaxis='my axis label 2')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.9" } }, "nbformat": 4, "nbformat_minor": 0 }