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

     Line 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, arange\n", "from sklearn import datasets\n", "from scipy.ndimage.filters import gaussian_filter\n", "from seaborn import color_palette" ] }, { "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": [ "##
One random line with default styles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To experience Lightning's custom zoom behaviors, try zooming and panning with the alt or command keys held down.\n", "
\n", "Alt will only zoom/pan in x (especially useful for time series), and command for y." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = gaussian_filter(random.rand(100), 3)\n", "lgn.line(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Setting line width and color" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For a single line you can pass one size and color." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = gaussian_filter(random.rand(100), 3)\n", "lgn.line(y, thickness=10, color=[255,100,100])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Multiple lines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Colors for multiple lines will automatically be assigned. Try hovering over a line to highlight it!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = gaussian_filter(random.rand(5,100), [0, 3])\n", "y = (y.T + arange(0,5)*0.2).T\n", "lgn.line(y, thickness=6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also set colors and thicknesses yourself, providing one per line. Here we do so using a palette from `seaborn`." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = gaussian_filter(random.rand(5,100), [0, 3])\n", "y = (y.T + arange(0,5)*0.2).T\n", "c = map(lambda x: list(asarray(x)*255), color_palette('Blues', 5))\n", "s = [8, 10, 12, 14, 16]\n", "lgn.line(y, thickness=s, color=c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Staggered lines and indices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's possible to show multiple lines of unequal length. \n", "
\n", "Here we also demonstrate passing an `index` to set the xaxis (we assume the `index` corresponds to the longest of the lines)." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y1 = gaussian_filter(random.rand(50), 5).tolist()\n", "y2 = gaussian_filter(random.rand(75), 5).tolist()\n", "y3 = gaussian_filter(random.rand(100), 5).tolist()\n", "x = range(50,150)\n", "lgn.line([y1,y2,y3], thickness=6, index=x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Clustered series with group labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of specifying colors directly as rgb, you can specify group assignments.\n", "
\n", "Here we use `scikitlearn` to generate clusters and then color according to cluster label." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d, g = datasets.make_blobs(n_features=5, n_samples=20, centers=5, cluster_std=1.0, random_state=100)\n", "lgn.line(d, group=g)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##
Axis labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also label the axes." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y = gaussian_filter(random.rand(100), 3)\n", "lgn.line(y, thickness=10, xaxis='variable #1', yaxis='variable #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 }