Skip to content

Instantly share code, notes, and snippets.

@Pabla007
Created June 1, 2019 17:42
Show Gist options
  • Save Pabla007/a231b57199190aa49d6f8cf9cfa08c86 to your computer and use it in GitHub Desktop.
Save Pabla007/a231b57199190aa49d6f8cf9cfa08c86 to your computer and use it in GitHub Desktop.
Brush Selector is a tool that just works like an hover tool in Bokeh Put i am used Bqplot to plot this.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#importing the necessary library\n",
"#from IPython.display import display\n",
"import pandas as pd\n",
"from bqplot.interacts import (BrushSelector)\n",
"from ipywidgets import VBox,HTML\n",
"from bqplot import LinearScale, Axis, Lines, Scatter, Figure\n",
"import numpy as np\n",
"\n",
"\n",
"symbol = 'Security 1'\n",
"symbol2 = 'Security 2'\n",
"\n",
"#Data genrated and making it as dataframe\n",
"price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.4], [0.4, 1.0]]), axis=0) + 100,\n",
" columns=['Security 1', 'Security 2'],\n",
" index=pd.date_range(start='01-01-2007', periods=150))\n",
"\n",
"dates_actual = price_data.index.values\n",
"prices = price_data[symbol].values\n",
"\n",
"#print(price_data)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "093b9a8688cb42d0b37744f088d58d20",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VkJveChjaGlsZHJlbj0oSFRNTCh2YWx1ZT11J1tdJyksIEZpZ3VyZShheGVzPVtBeGlzKGxhYmVsPXUnU2VjdXJpdHkgMScsIHNjYWxlPUxpbmVhclNjYWxlKCkpLCBBeGlzKGxhYmVsPXUnU2XigKY=\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#First we define a figure \n",
"\n",
"date_fmt = '%m-%d-%Y'\n",
"sec2_data = price_data[symbol2].values\n",
"dates = price_data.index.values\n",
"\n",
"sc_x = LinearScale()\n",
"sc_y = LinearScale()\n",
"\n",
"scatt = Scatter(x=prices, y=sec2_data,\n",
" scales={'x': sc_x, 'y': sc_y})\n",
"\n",
"sc_xax = Axis(label=(symbol), scale=sc_x)\n",
"sc_yax = Axis(label=(symbol2), scale=sc_y, orientation='vertical')\n",
"\n",
"br_sel = BrushSelector(x_scale=sc_x, y_scale=sc_y, marks=[scatt], color='red')\n",
"\n",
"\n",
"## We use the HTML widget to see the value of what we are selecting and modify it when an interaction is performed\n",
"## on the selector\n",
"\n",
"db_scat_brush = HTML(value='[]')\n",
"\n",
"## Now, we define a function that will be called when the selectors are interacted with - a callback\n",
"## call back for the selector\n",
"def brush_callback(change):\n",
" db_scat_brush.value = str(br_sel.selected)\n",
" \n",
"br_sel.observe(brush_callback, names=['brushing'])\n",
"\n",
"fig_scat_brush = Figure(marks=[scatt], axes=[sc_xax, sc_yax], title='Brush Selector By Pabla007',\n",
" interaction=br_sel)\n",
"\n",
"VBox([db_scat_brush, fig_scat_brush])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Soory !!!!! For the late work this will not happend again and i am ready for the next task :) :)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment