| 1 |
{ |
| 2 |
"cells": [ |
| 3 |
{ |
| 4 |
"cell_type": "code", |
| 5 |
"execution_count": null, |
| 6 |
"id": "juvenile-dialogue", |
| 7 |
"metadata": {}, |
| 8 |
"outputs": [], |
| 9 |
"source": [ |
| 10 |
"from bqplot import *\n", |
| 11 |
"\n", |
| 12 |
"import numpy as np\n", |
| 13 |
"import pandas as pd\n", |
| 14 |
"\n", |
| 15 |
"# Test data\n", |
| 16 |
"\n", |
| 17 |
"np.random.seed(0)\n", |
| 18 |
"price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[1.0, -0.8], [-0.8, 1.0]]), axis=0) + 100,\n", |
| 19 |
" columns=['Security 1', 'Security 2'], index=pd.date_range(start='01-01-2007', periods=150))\n", |
| 20 |
"size = 100\n", |
| 21 |
"x_data = range(size)\n", |
| 22 |
"y_data = np.cumsum(np.random.randn(size) * 100.0)\n", |
| 23 |
"ord_keys = np.array(['A', 'B', 'C', 'D', 'E', 'F'])\n", |
| 24 |
"ordinal_data = np.random.randint(5, size=size)\n", |
| 25 |
"\n", |
| 26 |
"symbols = ['Security 1', 'Security 2']\n", |
| 27 |
"\n", |
| 28 |
"dates_all = price_data.index.values\n", |
| 29 |
"dates_all_t = dates_all[1:]\n", |
| 30 |
"sec1_levels = np.array(price_data[symbols[0]].values.flatten())\n", |
| 31 |
"log_sec1 = np.log(sec1_levels)\n", |
| 32 |
"sec1_returns = log_sec1[1:] - log_sec1[:-1]\n", |
| 33 |
"\n", |
| 34 |
"sec2_levels = np.array(price_data[symbols[1]].values.flatten())\n", |
| 35 |
"\n", |
| 36 |
"# Create Scatter\n", |
| 37 |
"sc_x = DateScale()\n", |
| 38 |
"sc_y = LinearScale()\n", |
| 39 |
"\n", |
| 40 |
"scatt = Scatter(x=dates_all, y=sec2_levels, scales={'x': sc_x, 'y': sc_y})\n", |
| 41 |
"ax_x = Axis(scale=sc_x, label='Date')\n", |
| 42 |
"ax_y = Axis(scale=sc_y, orientation='vertical', tick_format='0.0f', label='Security 2')\n", |
| 43 |
"\n", |
| 44 |
"fig = Figure(marks=[scatt], axes=[ax_x, ax_y])\n", |
| 45 |
"fig" |
| 46 |
] |
| 47 |
}, |
| 48 |
{ |
| 49 |
"cell_type": "code", |
| 50 |
"execution_count": null, |
| 51 |
"id": "resistant-attachment", |
| 52 |
"metadata": {}, |
| 53 |
"outputs": [], |
| 54 |
"source": [ |
| 55 |
"# Update Scatter properties\n", |
| 56 |
"scatt.opacities = [0.3, 0.5, 1.]\n", |
| 57 |
"scatt.colors = ['green', 'red', 'blue']\n", |
| 58 |
"fig" |
| 59 |
] |
| 60 |
} |
| 61 |
], |
| 62 |
"metadata": { |
| 63 |
"kernelspec": { |
| 64 |
"display_name": "Python 3 (ipykernel)", |
| 65 |
"language": "python", |
| 66 |
"name": "python3" |
| 67 |
}, |
| 68 |
"language_info": { |
| 69 |
"codemirror_mode": { |
| 70 |
"name": "ipython", |
| 71 |
"version": 3 |
| 72 |
}, |
| 73 |
"file_extension": ".py", |
| 74 |
"mimetype": "text/x-python", |
| 75 |
"name": "python", |
| 76 |
"nbconvert_exporter": "python", |
| 77 |
"pygments_lexer": "ipython3", |
| 78 |
"version": "3.10.0" |
| 79 |
} |
| 80 |
}, |
| 81 |
"nbformat": 4, |
| 82 |
"nbformat_minor": 5 |
| 83 |
} |