Debian Codegraph @

bqplot/examples/Marks/Object Model/Candles.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": null,
6 "metadata": {
7 "input_collapsed": false
8 },
9 "outputs": [],
10 "source": [
11 "from bqplot import *\n",
12 "import numpy as np"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": null,
18 "metadata": {
19 "input_collapsed": false
20 },
21 "outputs": [],
22 "source": [
23 "price_data1 = [['2015-01-02 00:00:00', [161.31, 163.31, 161.0, 162.06]],\n",
24 " ['2015-01-05 00:00:00', [161.27, 161.27, 159.19, 159.51]],\n",
25 " ['2015-01-06 00:00:00', [159.67, 159.96, 155.17, 156.07]],\n",
26 " ['2015-01-07 00:00:00', [157.2, 157.2, 154.03, 155.05]],\n",
27 " ['2015-01-08 00:00:00', [156.24, 159.044, 155.55, 158.42]],\n",
28 " ['2015-01-09 00:00:00', [158.42, 160.34, 157.25, 159.11]],\n",
29 " ['2015-01-12 00:00:00', [159.0, 159.25, 155.76, 156.44]],\n",
30 " ['2015-01-13 00:00:00', [157.26, 159.97, 155.68, 156.81]],\n",
31 " ['2015-01-14 00:00:00', [154.86, 156.49, 153.74, 155.8]],\n",
32 " ['2015-01-15 00:00:00', [156.69, 156.97, 154.16, 154.57]]]\n",
33 "\n",
34 "price_data2 = [['2015-01-02 00:00:00', [111.39, 111.44, 107.35, 109.33]],\n",
35 " ['2015-01-05 00:00:00', [108.29, 108.65, 105.41, 106.25]],\n",
36 " ['2015-01-06 00:00:00', [106.54, 107.43, 104.63, 106.26]],\n",
37 " ['2015-01-07 00:00:00', [107.2, 108.2, 106.695, 107.75]],\n",
38 " ['2015-01-08 00:00:00', [109.23, 112.15, 108.7, 111.89]],\n",
39 " ['2015-01-09 00:00:00', [112.67, 113.25, 110.21, 112.01]],\n",
40 " ['2015-01-12 00:00:00', [112.6, 112.63, 108.8, 109.25]],\n",
41 " ['2015-01-13 00:00:00', [111.43, 112.8, 108.91, 110.22]],\n",
42 " ['2015-01-14 00:00:00', [109.04, 110.49, 108.5, 109.8]],\n",
43 " ['2015-01-15 00:00:00', [110.0, 110.06, 106.66, 106.82]]]"
44 ]
45 },
46 {
47 "cell_type": "code",
48 "execution_count": null,
49 "metadata": {},
50 "outputs": [],
51 "source": [
52 "# Split up the data into x and y points\n",
53 "from bqplot.traits import convert_to_date\n",
54 "\n",
55 "dates1 = convert_to_date([d[0] for d in price_data1])\n",
56 "prices1 = [d[1] for d in price_data1]\n",
57 "\n",
58 "dates2 = convert_to_date([d[0] for d in price_data2])\n",
59 "prices2 = [d[1] for d in price_data2]"
60 ]
61 },
62 {
63 "cell_type": "code",
64 "execution_count": null,
65 "metadata": {
66 "input_collapsed": false
67 },
68 "outputs": [],
69 "source": [
70 "sc = LinearScale()\n",
71 "dt_scale = DateScale()\n",
72 "ax_x = Axis(label='X', scale=dt_scale)\n",
73 "ax_y = Axis(label='Y', scale=sc, orientation='vertical', tick_format='0.0f')\n",
74 "\n",
75 "# Construct the marks\n",
76 "ohlc = OHLC(x=dates1, y=prices1, marker='candle', scales={'x': dt_scale, 'y': sc}, format='ohlc',\n",
77 " stroke='blue', display_legend=True, labels=['IBM US Equity'])\n",
78 "ohlc2 = OHLC(x=dates2, y=prices2, marker='bar', scales={'x': dt_scale, 'y': sc}, colors=['dodgerblue', 'orange'],\n",
79 " stroke='orange', display_legend=True, labels=['AAPL US Equity'], format='ohlc')\n",
80 "Figure(axes=[ax_x, ax_y], marks=[ohlc, ohlc2])"
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": null,
86 "metadata": {
87 "collapsed": true,
88 "input_collapsed": false
89 },
90 "outputs": [],
91 "source": [
92 "ohlc.colors = [None, 'red']"
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "metadata": {
99 "input_collapsed": false
100 },
101 "outputs": [],
102 "source": [
103 "sc_y = LinearScale()\n",
104 "sc_x = LogScale()\n",
105 "\n",
106 "ax_x = Axis(label='X', scale=sc_x)\n",
107 "ax_y = Axis(label='Y', scale=sc_y, orientation='vertical', tick_format='0.2f')\n",
108 "\n",
109 "ohlc3 = OHLC(x=np.arange(len(dates2)) + 1, y=np.array(prices2) / 60,\n",
110 " marker='bar', scales={'x': sc_x, 'y': sc_y}, colors=['dodgerblue','orange'])\n",
111 "\n",
112 "Figure(axes=[ax_x, ax_y], marks=[ohlc3])"
113 ]
114 },
115 {
116 "cell_type": "code",
117 "execution_count": null,
118 "metadata": {
119 "collapsed": true
120 },
121 "outputs": [],
122 "source": [
123 "ohlc3.marker = 'candle'"
124 ]
125 },
126 {
127 "cell_type": "markdown",
128 "metadata": {},
129 "source": [
130 "## OHLC with Ordinal Scale"
131 ]
132 },
133 {
134 "cell_type": "code",
135 "execution_count": null,
136 "metadata": {},
137 "outputs": [],
138 "source": [
139 "sc_x = OrdinalScale()\n",
140 "sc_y = LinearScale()\n",
141 "\n",
142 "ax_x = Axis(label='X', scale=sc_x, tick_format='%d-%m-%Y')\n",
143 "ax_y = Axis(label='Y', scale=sc_y, orientation='vertical', tick_format='0.2f')\n",
144 "\n",
145 "ohlc3 = OHLC(x=dates2, y=np.array(prices2) / 60, marker='candle', \n",
146 " scales={'x': sc_x, 'y': sc_y}, colors=['dodgerblue','orange'])\n",
147 "\n",
148 "Figure(axes=[ax_x, ax_y], marks=[ohlc3])"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": null,
154 "metadata": {
155 "collapsed": true
156 },
157 "outputs": [],
158 "source": [
159 "ohlc3.opacities = [0.1, 0.2]"
160 ]
161 },
162 {
163 "cell_type": "code",
164 "execution_count": null,
165 "metadata": {
166 "collapsed": true
167 },
168 "outputs": [],
169 "source": []
170 }
171 ],
172 "metadata": {
173 "anaconda-cloud": {},
174 "kernelspec": {
175 "display_name": "Python 3",
176 "language": "python",
177 "name": "python3"
178 },
179 "language_info": {
180 "codemirror_mode": {
181 "name": "ipython",
182 "version": 3
183 },
184 "file_extension": ".py",
185 "mimetype": "text/x-python",
186 "name": "python",
187 "nbconvert_exporter": "python",
188 "pygments_lexer": "ipython3",
189 "version": "3.6.1"
190 }
191 },
192 "nbformat": 4,
193 "nbformat_minor": 1
194 }