{ "metadata": { "name": "", "signature": "sha256:123d82ef0551f78e5dca94db6e00f1e10ae07d930467cf44709ccc6a9216776a" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Third Party Libraries With Rich Output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A number of third party libraries defined their own custom display logic. This gives their objcts rich output by default when used in the Notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Pandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Pandas](http://pandas.pydata.org/) is a data analysis library for Python. Its `DataFrame` objects have an HTML table representation in the Notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is a small amount of stock data for APPL:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%writefile data.csv\n", "Date,Open,High,Low,Close,Volume,Adj Close\n", "2012-06-01,569.16,590.00,548.50,584.00,14077000,581.50\n", "2012-05-01,584.90,596.76,522.18,577.73,18827900,575.26\n", "2012-04-02,601.83,644.00,555.00,583.98,28759100,581.48\n", "2012-03-01,548.17,621.45,516.22,599.55,26486000,596.99\n", "2012-02-01,458.41,547.61,453.98,542.44,22001000,540.12\n", "2012-01-03,409.40,458.24,409.00,456.48,12949100,454.53" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Writing data.csv\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read this as into a `DataFrame`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df = pandas.read_csv('data.csv')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And view the HTML representation:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "df" ], "language": "python", "metadata": {}, "outputs": [ { "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", "
DateOpenHighLowCloseVolumeAdj Close
0 2012-06-01 569.16 590.00 548.50 584.00 14077000 581.50
1 2012-05-01 584.90 596.76 522.18 577.73 18827900 575.26
2 2012-04-02 601.83 644.00 555.00 583.98 28759100 581.48
3 2012-03-01 548.17 621.45 516.22 599.55 26486000 596.99
4 2012-02-01 458.41 547.61 453.98 542.44 22001000 540.12
5 2012-01-03 409.40 458.24 409.00 456.48 12949100 454.53
\n", "

6 rows \u00d7 7 columns

\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ " Date Open High Low Close Volume Adj Close\n", "0 2012-06-01 569.16 590.00 548.50 584.00 14077000 581.50\n", "1 2012-05-01 584.90 596.76 522.18 577.73 18827900 575.26\n", "2 2012-04-02 601.83 644.00 555.00 583.98 28759100 581.48\n", "3 2012-03-01 548.17 621.45 516.22 599.55 26486000 596.99\n", "4 2012-02-01 458.41 547.61 453.98 542.44 22001000 540.12\n", "5 2012-01-03 409.40 458.24 409.00 456.48 12949100 454.53\n", "\n", "[6 rows x 7 columns]" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "SymPy" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[SymPy](http://sympy.org/) is a symbolic computing library for Python. Its equation objects have LaTeX representations that are rendered in the Notebook." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from sympy.interactive.printing import init_printing\n", "init_printing(use_latex='mathjax')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import division\n", "import sympy as sym\n", "from sympy import *\n", "x, y, z = symbols(\"x y z\")\n", "k, m, n = symbols(\"k m n\", integer=True)\n", "f, g, h = map(Function, 'fgh')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 14 }, { "cell_type": "code", "collapsed": false, "input": [ "Rational(3,2)*pi + exp(I*x) / (x**2 + y)" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\frac{3 \\pi}{2} + \\frac{e^{i x}}{x^{2} + y}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ " \u2148\u22c5x \n", "3\u22c5\u03c0 \u212f \n", "\u2500\u2500\u2500 + \u2500\u2500\u2500\u2500\u2500\u2500\n", " 2 2 \n", " x + y" ] } ], "prompt_number": 15 }, { "cell_type": "code", "collapsed": false, "input": [ "a = 1/x + (x*sin(x) - 1)/x\n", "a" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$\\frac{1}{x} \\left(x \\sin{\\left (x \\right )} - 1\\right) + \\frac{1}{x}$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "x\u22c5sin(x) - 1 1\n", "\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 + \u2500\n", " x x" ] } ], "prompt_number": 16 }, { "cell_type": "code", "collapsed": false, "input": [ "(1/cos(x)).series(x, 0, 6)" ], "language": "python", "metadata": {}, "outputs": [ { "latex": [ "$$1 + \\frac{x^{2}}{2} + \\frac{5 x^{4}}{24} + \\mathcal{O}\\left(x^{6}\\right)$$" ], "metadata": {}, "output_type": "pyout", "prompt_number": 17, "text": [ " 2 4 \n", " x 5\u22c5x \u239b 6\u239e\n", "1 + \u2500\u2500 + \u2500\u2500\u2500\u2500 + O\u239dx \u23a0\n", " 2 24 " ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Vincent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Vincent](https://vincent.readthedocs.org/en/latest/) is a visualization library that uses the [Vega](http://trifacta.github.io/vega/) visualization grammar to build [d3.js](http://d3js.org/) based visualizations in the Notebook and on http://nbviewer.ipython.org. `Visualization` objects in Vincetn have rich HTML and JavaSrcript representations." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import vincent\n", "import pandas as pd" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import pandas.io.data as web\n", "import datetime\n", "all_data = {}\n", "date_start = datetime.datetime(2010, 1, 1)\n", "date_end = datetime.datetime(2014, 1, 1)\n", "for ticker in ['AAPL', 'IBM', 'YHOO', 'MSFT']:\n", " all_data[ticker] = web.DataReader(ticker, 'yahoo', date_start, date_end)\n", "price = pd.DataFrame({tic: data['Adj Close']\n", " for tic, data in all_data.items()})" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "vincent.initialize_notebook()" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "line = vincent.Line(price[['AAPL', 'IBM', 'YHOO', 'MSFT']], width=600, height=300)\n", "line.axis_titles(x='Date', y='Price')\n", "line.legend(title='Ticker')\n", "display(line)" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", "\n", " " ], "metadata": {}, "output_type": "display_data", "text": [ "" ] } ], "prompt_number": 10 } ], "metadata": {} } ] }