Created
January 22, 2015 06:13
-
-
Save chriddyp/49b0a5284f02b69dcc14 to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:5ed957545c082c7086bce8dfdef0c2103f84fcd452ce0dbfd96c65faa92f3c06" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import plotly.plotly as py\n", | |
"from plotly.graph_objs import *\n", | |
"import plotly.tools as tls\n", | |
"\n", | |
"import json\n", | |
"import numpy as np\n", | |
"import math\n", | |
"from IPython.display import display" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from plotly.widgets import Graph" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"contour_plot = Graph('https://plot.ly/~bronsolo/63')\n", | |
"line_plot = Graph('https://plot.ly/~chris/2150')\n", | |
"\n", | |
"display(contour_plot)\n", | |
"display(line_plot)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"contour_fig = py.get_figure('https://plot.ly/~bronsolo/63')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def march(x0, y0, x1, y1):\n", | |
" '''\n", | |
" Return the closest path of integer coordinates \n", | |
" between (x0, y0) and (x1, y1)\n", | |
" '''\n", | |
" if abs(x1-x0) > abs(y1-y0): \n", | |
" if x1>x0:\n", | |
" x = range(int(x0), int(x1)+1)\n", | |
" else:\n", | |
" x = range(int(x0), int(x1)+1, -1)\n", | |
" y = []\n", | |
" tanth = (y1-y0)/(x1-x0)\n", | |
" for xi in x:\n", | |
" y.append(round(y0+(xi-x0)*tanth))\n", | |
" else:\n", | |
" if y1>y0:\n", | |
" y = range(int(y0), int(y1)+1)\n", | |
" else:\n", | |
" y = range(int(y0), int(y1)+1, -1)\n", | |
" x = []\n", | |
" tanth = (x1-x0)/(y1-y0)\n", | |
" for yi in y:\n", | |
" x.append(round(x0+(yi-y0)*tanth))\n", | |
"\n", | |
" return (x, y)\n", | |
"\n", | |
"class Responder(object):\n", | |
" '''\n", | |
" Stateful object that stores and computes the \n", | |
" elevation and distance data of the \n", | |
" line plot. The 'click' method is executed\n", | |
" on `click` events of the contour map.\n", | |
" '''\n", | |
" def __init__(self, data):\n", | |
" self.clicks = 0\n", | |
" self.x = []\n", | |
" self.y = []\n", | |
" self.xp = []\n", | |
" self.yp = []\n", | |
" self.z = []\n", | |
" self.d = []\n", | |
" self.data = data\n", | |
"\n", | |
" def append_point(self, point):\n", | |
" xp = point['pointNumber'][1]\n", | |
" yp = point['pointNumber'][0]\n", | |
" self.xp.append(xp)\n", | |
" self.yp.append(yp)\n", | |
"\n", | |
" if 'x' in self.data[0]:\n", | |
" x = self.data[0]['x'][xp]\n", | |
" else:\n", | |
" x = xp\n", | |
" if 'y' in self.data[0]:\n", | |
" y = self.data[0]['y'][xp]\n", | |
" else:\n", | |
" y = yp\n", | |
"\n", | |
" self.x.append(x)\n", | |
" self.y.append(y)\n", | |
" self.z.append(point['z'])\n", | |
" \n", | |
" if len(self.x) == 1:\n", | |
" self.d.append(0)\n", | |
" else:\n", | |
" self.d.append(math.sqrt((y-self.y[-2])**2+(x-self.x[-2])**2))\n", | |
" \n", | |
" \n", | |
" def click(self, contour_widget, click_obj):\n", | |
" point = click_obj[0]\n", | |
" if self.clicks==0 or self.clicks > 5:\n", | |
" self.__init__(self.data)\n", | |
" self.append_point(point)\n", | |
" else:\n", | |
" \n", | |
" (xpath, ypath) = march(self.xp[-1], self.yp[-1],\n", | |
" point['pointNumber'][1], point['pointNumber'][0])\n", | |
" \n", | |
" d = []\n", | |
" z = []\n", | |
" for i in range(1, len(xpath)):\n", | |
" xpi = xpath[i]\n", | |
" ypi = ypath[i]\n", | |
"\n", | |
" if 'x' in self.data[0]:\n", | |
" xi = self.data[0]['x'][xpi]\n", | |
" else:\n", | |
" xi = xpi\n", | |
" if 'y' in self.data[0]:\n", | |
" yi = self.data[0]['y'][ypi]\n", | |
" else:\n", | |
" yi = ypi\n", | |
" \n", | |
" self.d.append(self.d[-1]+math.sqrt((yi-self.y[-1])**2+(xi-self.x[-1])**2))\n", | |
" self.z.append(self.data[0]['z'][int(ypi)][int(xpi)])\n", | |
" self.x.append(xi)\n", | |
" self.y.append(yi)\n", | |
" self.xp.append(xpi)\n", | |
" self.yp.append(ypi)\n", | |
" \n", | |
"\n", | |
" self.clicks+=1\n", | |
" \n", | |
" line_plot.restyle({'x': [self.d], 'y': [self.z]})\n", | |
"\n", | |
" \n", | |
"r = Responder(contour_fig['data'])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"contour_plot.on_click(r.click)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 20 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment