Last active
September 27, 2020 21:49
-
-
Save liaskast/87c30debb5420b57fc2b36735f5ca991 to your computer and use it in GitHub Desktop.
Bqplot in HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This file is autogenerated by pip-compile | |
# To update, run: | |
# | |
# pip-compile | |
# | |
numpy>=1, <2 | |
scipy>=1, <2 | |
IPython>=6, <8 | |
nbconvert>=5.3, <6 | |
traitlets>=4.3, <5 | |
toolz>=0.8, <1 | |
docopt>=0.6.2, <1 | |
nbformat>=4.4.0, <5 | |
Jinja2>=2.10, <3 | |
bqplot==0.11.0 | |
ipywidgets>=7.5.0, <8 | |
xlrd>= 1.0.0, <2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from __future__ import print_function\n", | |
"import ipywidgets as widgets\n", | |
"from bqplot import pyplot as plt\n", | |
"from bqplot import topo_load\n", | |
"from bqplot.interacts import panzoom\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"import datetime as dt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# initializing data to be plotted\n", | |
"np.random.seed(0)\n", | |
"size = 100\n", | |
"y_data = np.cumsum(np.random.randn(size) * 100.0)\n", | |
"y_data_2 = np.cumsum(np.random.randn(size))\n", | |
"y_data_3 = np.cumsum(np.random.randn(size) * 100.)\n", | |
"\n", | |
"x = np.linspace(0.0, 10.0, size)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,\n", | |
" columns=['Security 1', 'Security 2'],\n", | |
" index=pd.date_range(start='01-01-2007', periods=150))\n", | |
"\n", | |
"symbol = 'Security 1'\n", | |
"dates_all = price_data.index.values\n", | |
"final_prices = price_data[symbol].values.flatten()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"price_data.index.names = ['date']" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Simple Plots" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Line Chart" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot(x, y_data)\n", | |
"plt.xlabel('Time')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"_ = plt.ylabel('Stock Price')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Setting the title for the current figure\n", | |
"plt.title('Brownian Increments')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot('Security 1', data=price_data)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Scatter Plot" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure(title='Scatter Plot with colors')\n", | |
"plt.scatter(y_data_2, y_data_3, color=y_data, stroke='black')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Horizontal and Vertical Lines" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## adding a horizontal line at y=0\n", | |
"plt.hline(0)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## adding a vertical line at x=4 with stroke_width and colors being passed.\n", | |
"plt.vline(4., stroke_width=2, colors=['orangered'])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.scatter('Security 1', 'Security 2', color='date', data=price_data.reset_index(), stroke='black')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Histogram" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.hist(y_data, colors=['OrangeRed'])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.hist('Security 1', data=price_data, colors=['MediumSeaGreen'])\n", | |
"plt.xlabel('Hello')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Bar Chart" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"bar_x=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U']\n", | |
"plt.bar(bar_x, y_data_3)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.bar('date', 'Security 2', data=price_data.reset_index()[:10])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Pie Chart" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"d = abs(y_data_2[:5])\n", | |
"plt.pie(d)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.pie('Security 2', color='Security 1', data=price_data[:4])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### OHLC" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dates = np.arange(dt.datetime(2014, 1, 2), dt.datetime(2014, 1, 30), dt.timedelta(days=1))\n", | |
"\n", | |
"prices = np.array([[ 187.21 , 187.4 , 185.2 , 185.53 ],\n", | |
" [ 185.83 , 187.35 , 185.3 , 186.64 ],\n", | |
" [ 187.15 , 187.355 , 185.3 , 186. ],\n", | |
" [ 186.39 , 190.35 , 186.38 , 189.71 ],\n", | |
" [ 189.33 , 189.4175, 187.26 , 187.97 ],\n", | |
" [ 189.02 , 189.5 , 186.55 , 187.38 ],\n", | |
" [ 188.31 , 188.57 , 186.28 , 187.26 ],\n", | |
" [ 186.26 , 186.95 , 183.86 , 184.16 ],\n", | |
" [ 185.06 , 186.428 , 183.8818, 185.92 ],\n", | |
" [ 185.82 , 188.65 , 185.49 , 187.74 ],\n", | |
" [ 187.53 , 188.99 , 186.8 , 188.76 ],\n", | |
" [ 188.04 , 190.81 , 187.86 , 190.09 ],\n", | |
" [ 190.23 , 190.39 , 186.79 , 188.43 ],\n", | |
" [ 181.28 , 183.5 , 179.67 , 182.25 ],\n", | |
" [ 181.43 , 183.72 , 180.71 , 182.73 ],\n", | |
" [ 181.25 , 182.8141, 179.64 , 179.64 ],\n", | |
" [ 179.605 , 179.65 , 177.66 , 177.9 ],\n", | |
" [ 178.05 , 178.45 , 176.16 , 176.85 ],\n", | |
" [ 175.98 , 178.53 , 175.89 , 176.4 ],\n", | |
" [ 177.17 , 177.86 , 176.36 , 177.36 ]])\n", | |
"\n", | |
"plt.figure()\n", | |
"plt.ohlc(dates, prices)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Boxplot" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.boxplot(np.arange(10), np.random.randn(10, 100))\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Map" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.geo(map_data='WorldMap')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Heatmap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure(padding_y=0)\n", | |
"plt.heatmap(x * x[:, np.newaxis])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### GridHeatMap" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure(padding_y=0)\n", | |
"plt.gridheatmap(x[:10] * x[:10, np.newaxis])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Plotting Dates" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot(dates_all, final_prices)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Editing existing axes properties" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## adding grid lines and changing the side of the axis in the figure above\n", | |
"_ = plt.axes(options={'x': {'grid_lines': 'solid'}, 'y': {'side': 'right', 'grid_lines': 'dashed'}})" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Advanced Usage" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Multiple Marks on the same Figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot(x, y_data_3, colors=['orangered'])\n", | |
"plt.scatter(x, y_data, stroke='black')\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Using marker strings in Line Chart" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"mark_x = np.arange(10)\n", | |
"plt.figure(title='Using Marker Strings')\n", | |
"plt.plot(mark_x, 3 * mark_x + 5, 'g-.s') # color=green, line_style=dash_dotted, marker=square\n", | |
"plt.plot(mark_x ** 2, 'm:d') # color=magenta, line_style=None, marker=diamond\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"collapsed": true | |
}, | |
"source": [ | |
"### Partially changing the scales" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"plt.plot(x, y_data)\n", | |
"\n", | |
"## preserving the x scale and changing the y scale\n", | |
"plt.scales(scales={'x': plt.Keep})\n", | |
"plt.plot(x, y_data_2, colors=['orange'], axes_options={'y': {'side': 'right', 'color': 'orange',\n", | |
" 'grid_lines': 'none'}})\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Adding a label to the chart" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure()\n", | |
"line = plt.plot(dates_all, final_prices)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## adds the label to the figure created above\n", | |
"_ = plt.label(['Pie Day'], x=[np.datetime64('2007-03-14')], y=[final_prices.mean()], scales=line.scales,\n", | |
" colors=['orangered'])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Changing context figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure(1)\n", | |
"plt.plot(x,y_data_3)\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.figure(2)\n", | |
"plt.plot(x[:20],y_data_3[:20])\n", | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Re-editing first figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"## adds the new line to the first figure\n", | |
"fig = plt.figure(1, title='New title')\n", | |
"plt.plot(x,y_data, colors=['orangered'])\n", | |
"fig" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Viewing the properties of the figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"marks = plt.current_figure().marks\n", | |
"marks[0].get_state()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Showing a second view of the first figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.show()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Clearing the figure" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"### Clearing the figure above\n", | |
"plt.clear()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Deleting a figure and all its views. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.show(2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"plt.close(2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Interactions in Pyplot" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Brush Selector" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"fig = plt.figure()\n", | |
"plt.scatter(y_data_2, y_data_3, colors=['orange'], stroke='black')\n", | |
"\n", | |
"label = widgets.Label()\n", | |
"\n", | |
"def callback(name, value):\n", | |
" label.value = str(value)\n", | |
"## click and drag on the figure to see the selector\n", | |
"plt.brush_selector(callback)\n", | |
"\n", | |
"widgets.VBox([fig, label])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Fast Interval Selector" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"fig = plt.figure()\n", | |
"n = 100\n", | |
"plt.plot(np.arange(n), y_data_3)\n", | |
"label = widgets.Label()\n", | |
"\n", | |
"def callback(name, value):\n", | |
" label.value = str(value)\n", | |
"\n", | |
"## click on the figure to activate the selector\n", | |
"plt.int_selector(callback)\n", | |
"\n", | |
"widgets.VBox([fig, label])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Brush Interval Selector with call back on brushing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# click and drag on chart to make a selection\n", | |
"def callback(name, value):\n", | |
" label.value = 'Brushing: ' + str(value)\n", | |
"_ = plt.brush_int_selector(callback, 'brushing')" | |
] | |
} | |
], | |
"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.7.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment