{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "input_collapsed": false }, "outputs": [], "source": [ "from bqplot import *\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "input_collapsed": false }, "outputs": [], "source": [ "price_data1 = [['2015-01-02 00:00:00', [161.31, 163.31, 161.0, 162.06]],\n", " ['2015-01-05 00:00:00', [161.27, 161.27, 159.19, 159.51]],\n", " ['2015-01-06 00:00:00', [159.67, 159.96, 155.17, 156.07]],\n", " ['2015-01-07 00:00:00', [157.2, 157.2, 154.03, 155.05]],\n", " ['2015-01-08 00:00:00', [156.24, 159.044, 155.55, 158.42]],\n", " ['2015-01-09 00:00:00', [158.42, 160.34, 157.25, 159.11]],\n", " ['2015-01-12 00:00:00', [159.0, 159.25, 155.76, 156.44]],\n", " ['2015-01-13 00:00:00', [157.26, 159.97, 155.68, 156.81]],\n", " ['2015-01-14 00:00:00', [154.86, 156.49, 153.74, 155.8]],\n", " ['2015-01-15 00:00:00', [156.69, 156.97, 154.16, 154.57]]]\n", "\n", "price_data2 = [['2015-01-02 00:00:00', [111.39, 111.44, 107.35, 109.33]],\n", " ['2015-01-05 00:00:00', [108.29, 108.65, 105.41, 106.25]],\n", " ['2015-01-06 00:00:00', [106.54, 107.43, 104.63, 106.26]],\n", " ['2015-01-07 00:00:00', [107.2, 108.2, 106.695, 107.75]],\n", " ['2015-01-08 00:00:00', [109.23, 112.15, 108.7, 111.89]],\n", " ['2015-01-09 00:00:00', [112.67, 113.25, 110.21, 112.01]],\n", " ['2015-01-12 00:00:00', [112.6, 112.63, 108.8, 109.25]],\n", " ['2015-01-13 00:00:00', [111.43, 112.8, 108.91, 110.22]],\n", " ['2015-01-14 00:00:00', [109.04, 110.49, 108.5, 109.8]],\n", " ['2015-01-15 00:00:00', [110.0, 110.06, 106.66, 106.82]]]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Split up the data into x and y points\n", "from bqplot.traits import convert_to_date\n", "\n", "dates1 = convert_to_date([d[0] for d in price_data1])\n", "prices1 = [d[1] for d in price_data1]\n", "\n", "dates2 = convert_to_date([d[0] for d in price_data2])\n", "prices2 = [d[1] for d in price_data2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "input_collapsed": false }, "outputs": [], "source": [ "sc = LinearScale()\n", "dt_scale = DateScale()\n", "ax_x = Axis(label='X', scale=dt_scale)\n", "ax_y = Axis(label='Y', scale=sc, orientation='vertical', tick_format='0.0f')\n", "\n", "# Construct the marks\n", "ohlc = OHLC(x=dates1, y=prices1, marker='candle', scales={'x': dt_scale, 'y': sc}, format='ohlc',\n", " stroke='blue', display_legend=True, labels=['IBM US Equity'])\n", "ohlc2 = OHLC(x=dates2, y=prices2, marker='bar', scales={'x': dt_scale, 'y': sc}, colors=['dodgerblue', 'orange'],\n", " stroke='orange', display_legend=True, labels=['AAPL US Equity'], format='ohlc')\n", "Figure(axes=[ax_x, ax_y], marks=[ohlc, ohlc2])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "input_collapsed": false }, "outputs": [], "source": [ "ohlc.colors = [None, 'red']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "input_collapsed": false }, "outputs": [], "source": [ "sc_y = LinearScale()\n", "sc_x = LogScale()\n", "\n", "ax_x = Axis(label='X', scale=sc_x)\n", "ax_y = Axis(label='Y', scale=sc_y, orientation='vertical', tick_format='0.2f')\n", "\n", "ohlc3 = OHLC(x=np.arange(len(dates2)) + 1, y=np.array(prices2) / 60,\n", " marker='bar', scales={'x': sc_x, 'y': sc_y}, colors=['dodgerblue','orange'])\n", "\n", "Figure(axes=[ax_x, ax_y], marks=[ohlc3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ohlc3.marker = 'candle'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## OHLC with Ordinal Scale" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sc_x = OrdinalScale()\n", "sc_y = LinearScale()\n", "\n", "ax_x = Axis(label='X', scale=sc_x, tick_format='%d-%m-%Y')\n", "ax_y = Axis(label='Y', scale=sc_y, orientation='vertical', tick_format='0.2f')\n", "\n", "ohlc3 = OHLC(x=dates2, y=np.array(prices2) / 60, marker='candle', \n", " scales={'x': sc_x, 'y': sc_y}, colors=['dodgerblue','orange'])\n", "\n", "Figure(axes=[ax_x, ax_y], marks=[ohlc3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ohlc3.opacities = [0.1, 0.2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }