{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import altair.vegalite.v2 as vl\n", "from altair.datasets import load_dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "cars = load_dataset('cars')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AccelerationCylindersDisplacementHorsepowerMiles_per_GallonNameOriginWeight_in_lbsYear
012.08307.0130.018.0chevrolet chevelle malibuUSA35041970-01-01
111.58350.0165.015.0buick skylark 320USA36931970-01-01
211.08318.0150.018.0plymouth satelliteUSA34361970-01-01
312.08304.0150.016.0amc rebel sstUSA34331970-01-01
410.58302.0140.017.0ford torinoUSA34491970-01-01
\n", "
" ], "text/plain": [ " Acceleration Cylinders Displacement Horsepower Miles_per_Gallon \\\n", "0 12.0 8 307.0 130.0 18.0 \n", "1 11.5 8 350.0 165.0 15.0 \n", "2 11.0 8 318.0 150.0 18.0 \n", "3 12.0 8 304.0 150.0 16.0 \n", "4 10.5 8 302.0 140.0 17.0 \n", "\n", " Name Origin Weight_in_lbs Year \n", "0 chevrolet chevelle malibu USA 3504 1970-01-01 \n", "1 buick skylark 320 USA 3693 1970-01-01 \n", "2 plymouth satellite USA 3436 1970-01-01 \n", "3 amc rebel sst USA 3433 1970-01-01 \n", "4 ford torino USA 3449 1970-01-01 " ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cars.head()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vl.data_transformers.enable('json')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "spec = {\n", " \"data\": vl.pipe(cars, vl.data_transformers.get()),\n", " \"vconcat\": [{\n", " \"selection\": {\n", " \"brush\": {\"type\": \"interval\"}\n", " },\n", " \"mark\": \"point\",\n", " \"encoding\": {\n", " \"x\": {\"field\": \"Horsepower\",\"type\": \"quantitative\"},\n", " \"y\": {\"field\": \"Miles_per_Gallon\",\"type\": \"quantitative\"}\n", " }\n", " }, {\n", " \"transform\": [\n", " {\"filter\": {\"selection\": \"brush\"}}\n", " ],\n", " \"mark\": \"point\",\n", " \"encoding\": {\n", " \"x\": {\n", " \"field\": \"Acceleration\", \"type\": \"quantitative\",\n", " \"scale\": {\"domain\": [0,25]}\n", " },\n", " \"y\": {\n", " \"field\": \"Displacement\",\"type\": \"quantitative\",\n", " \"scale\": {\"domain\": [0, 500]}\n", " }\n", " }\n", " }]\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To render in the classic notebook run this line:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vg.renderers.enable('notebook')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To render in JupyterLab and nteract, run this" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vg.renderers.enable('default')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "({'application/vnd.vegalite.v2+json': {'data': {'format': {'type': 'json'},\n", " 'url': '/files/github/altair-viz/altair/notebooks/altair-data-0004ed27-fa48-4358-8bac-bea454ea1c74.json'},\n", " 'vconcat': [{'encoding': {'x': {'field': 'Horsepower',\n", " 'type': 'quantitative'},\n", " 'y': {'field': 'Miles_per_Gallon', 'type': 'quantitative'}},\n", " 'mark': 'point',\n", " 'selection': {'brush': {'type': 'interval'}}},\n", " {'encoding': {'x': {'field': 'Acceleration',\n", " 'scale': {'domain': [0, 25]},\n", " 'type': 'quantitative'},\n", " 'y': {'field': 'Displacement',\n", " 'scale': {'domain': [0, 500]},\n", " 'type': 'quantitative'}},\n", " 'mark': 'point',\n", " 'transform': [{'filter': {'selection': 'brush'}}]}]},\n", " 'text/plain': ''},\n", " {})" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vl.renderers.get()(spec)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.vegalite.v2+json": { "data": { "format": { "type": "json" }, "url": "/files/github/altair-viz/altair/notebooks/altair-data-0004ed27-fa48-4358-8bac-bea454ea1c74.json" }, "vconcat": [ { "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" } }, "mark": "point", "selection": { "brush": { "encodings": [ "x", "y" ], "mark": { "fill": "#333", "fillOpacity": 0.125, "stroke": "white" }, "on": "[mousedown, window:mouseup] > window:mousemove!", "resolve": "global", "translate": "[mousedown, window:mouseup] > window:mousemove!", "type": "interval", "zoom": "wheel!" } } }, { "encoding": { "x": { "field": "Acceleration", "scale": { "domain": [ 0, 25 ] }, "type": "quantitative" }, "y": { "field": "Displacement", "scale": { "domain": [ 0, 500 ] }, "type": "quantitative" } }, "mark": "point", "transform": [ { "filter": { "selection": "brush" } } ] } ] }, "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "vl.vegalite(spec, validate=True)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "vl.renderers.enable('json')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/json": { "data": { "format": { "type": "json" }, "url": "/files/github/altair-viz/altair/notebooks/altair-data-0004ed27-fa48-4358-8bac-bea454ea1c74.json" }, "vconcat": [ { "encoding": { "x": { "field": "Horsepower", "type": "quantitative" }, "y": { "field": "Miles_per_Gallon", "type": "quantitative" } }, "mark": "point", "selection": { "brush": { "type": "interval" } } }, { "encoding": { "x": { "field": "Acceleration", "scale": { "domain": [ 0, 25 ] }, "type": "quantitative" }, "y": { "field": "Displacement", "scale": { "domain": [ 0, 500 ] }, "type": "quantitative" } }, "mark": "point", "transform": [ { "filter": { "selection": "brush" } } ] } ] }, "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "vl.vegalite(spec, validate=True)" ] } ], "metadata": { "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }