Skip to content

Instantly share code, notes, and snippets.

@hafen
Created September 30, 2015 20:37
Show Gist options
  • Save hafen/04444a8f8028f88654fe to your computer and use it in GitHub Desktop.
Save hafen/04444a8f8028f88654fe to your computer and use it in GitHub Desktop.
Basketball Overlay

Created by Ryan Hafen

library(rbokeh)
url <- 'https://raw.githubusercontent.com/weinfz/nba_shot_value_charts/master/court.png'
p <- figure(width = 600, height = 600 * aspect, padding_factor = 0, 
  xlab = NULL, ylab = NULL) %>%
  ly_image_url(x = -300, y = -20, url = url, w = 600, h = 440, anchor = 'bottom_left') %>%
  ly_points(rnorm(100, 0, 50), rnorm(100, 200, 50), color = 'black')
p
<!DOCTYPE html>
<html>
<head>
<script src="http://cdn.pydata.org/bokeh/release/bokeh-0.10.0.min.js"></script>
<link href="http://cdn.pydata.org/bokeh/release/bokeh-0.10.0.min.css" rel="stylesheet">
</head>
<body>
<div id="9e85f6332c1d2ed3773b91462d76a3aa" class="plotdiv"></div>
<script type="text/javascript">
Bokeh.$(function() {
var modelid = "e19bb818e355851e187e0e8a5d606981";
var modeltype = "Plot";
var elementid = "9e85f6332c1d2ed3773b91462d76a3aa";
Bokeh.logger.info("Realizing plot:");
Bokeh.logger.info(" - modeltype: Plot");
Bokeh.logger.info(" - modelid: e19bb818e355851e187e0e8a5d606981");
Bokeh.logger.info(" - elementid: 9e85f6332c1d2ed3773b91462d76a3aa");
var all_models = [{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","attributes":{"title":null,"id":"e19bb818e355851e187e0e8a5d606981","plot_width":590,"plot_height":469.8442,"x_range":{"type":"Range1d","id":"826cb861f0509ffbaee501d99d2aa290"},"y_range":{"type":"Range1d","id":"b8360254b37f3595a1d243d2edfe3564"},"left":[{"type":"LinearAxis","id":"7b5d7f1ec855f57c4ae6c4107bcc962f"}],"below":[{"type":"LinearAxis","id":"41a02c7a5a7850d9c2079010a416f695"}],"right":[],"above":[],"renderers":[{"type":"GlyphRenderer","id":"1312347a475d35962b8a71d52397bb43"},{"type":"GlyphRenderer","id":"a1f75ced3f0cf6dc7d6158af0eb2858c"},{"type":"LinearAxis","id":"41a02c7a5a7850d9c2079010a416f695"},{"type":"Grid","id":"16e40bc110844322e83a4ee8bc832566"},{"type":"LinearAxis","id":"7b5d7f1ec855f57c4ae6c4107bcc962f"},{"type":"Grid","id":"7bb8638bb976be601c65154ae6f213ad"}],"tools":[{"type":"PanTool","id":"b0bcde1b37713d9990a17709baa4fdc0"},{"type":"WheelZoomTool","id":"52a5696e1dc66f6bfd1166d945540944"},{"type":"BoxZoomTool","id":"ea0231ef6c8d2a5861cc50e852799b8e"},{"type":"ResizeTool","id":"92cb6a73edb2ccce35d704ce2fda3b32"},{"type":"ResetTool","id":"8dc0ba4765e16b8897bac30b24cae6e5"},{"type":"PreviewSaveTool","id":"124247303430ba849c2078759129d3ab"},{"type":"HelpTool","id":"904a931f750661da79836c8561ae3aee"}],"tool_events":{"type":"ToolEvents","id":"d70472076b131f411b6946bc8f8211ad"},"extra_y_ranges":{},"extra_x_ranges":{},"tags":[],"doc":null,"min_border":4,"lod_threshold":null},"subtype":"Figure"},{"type":"PanTool","id":"b0bcde1b37713d9990a17709baa4fdc0","attributes":{"id":"b0bcde1b37713d9990a17709baa4fdc0","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"dimensions":["width","height"]}},{"type":"ToolEvents","id":"d70472076b131f411b6946bc8f8211ad","attributes":{"id":"d70472076b131f411b6946bc8f8211ad","tags":[],"doc":null},"geometries":[]},{"type":"WheelZoomTool","id":"52a5696e1dc66f6bfd1166d945540944","attributes":{"id":"52a5696e1dc66f6bfd1166d945540944","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"dimensions":["width","height"]}},{"type":"BoxZoomTool","id":"ea0231ef6c8d2a5861cc50e852799b8e","attributes":{"id":"ea0231ef6c8d2a5861cc50e852799b8e","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"}}},{"type":"ResizeTool","id":"92cb6a73edb2ccce35d704ce2fda3b32","attributes":{"id":"92cb6a73edb2ccce35d704ce2fda3b32","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"}}},{"type":"ResetTool","id":"8dc0ba4765e16b8897bac30b24cae6e5","attributes":{"id":"8dc0ba4765e16b8897bac30b24cae6e5","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"}}},{"type":"PreviewSaveTool","id":"124247303430ba849c2078759129d3ab","attributes":{"id":"124247303430ba849c2078759129d3ab","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"}}},{"type":"HelpTool","id":"904a931f750661da79836c8561ae3aee","attributes":{"id":"904a931f750661da79836c8561ae3aee","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"redirect":["http://hafen.github.io/rbokeh"],"help_tooltip":["Click to learn more about rbokeh."]}},{"type":"ColumnDataSource","id":"8892beed85277815ac6190eb37bc0437","attributes":{"id":"8892beed85277815ac6190eb37bc0437","tags":[],"doc":null,"column_names":["dummy"],"selected":[],"discrete_ranges":{},"cont_ranges":{},"data":{"dummy":[1]}}},{"type":"ImageURL","id":"44a12163faae8c5a53e4ec2b65382a1b","attributes":{"id":"44a12163faae8c5a53e4ec2b65382a1b","tags":[],"doc":null,"url":{"units":"data","value":"https://raw.githubusercontent.com/weinfz/nba_shot_value_charts/master/court.png"},"w":{"units":"data","value":600},"h":{"units":"data","value":440},"anchor":"bottom_left","angle":{"units":"rad","value":0},"dilate":true,"x":{"units":"data","value":-300},"y":{"units":"data","value":-20}}},{"type":"ImageURL","id":"4bf389aa18553da77253ec73464eb76c","attributes":{"id":"4bf389aa18553da77253ec73464eb76c","tags":[],"doc":null,"url":{"units":"data","value":"https://raw.githubusercontent.com/weinfz/nba_shot_value_charts/master/court.png"},"w":{"units":"data","value":600},"h":{"units":"data","value":440},"anchor":"bottom_left","angle":{"units":"rad","value":0},"dilate":true,"x":{"units":"data","value":-300},"y":{"units":"data","value":-20}}},{"type":"GlyphRenderer","id":"1312347a475d35962b8a71d52397bb43","attributes":{"id":"1312347a475d35962b8a71d52397bb43","tags":[],"doc":null,"selection_glyph":null,"nonselection_glyph":{"type":"ImageURL","id":"4bf389aa18553da77253ec73464eb76c"},"server_data_source":null,"name":null,"data_source":{"type":"ColumnDataSource","id":"8892beed85277815ac6190eb37bc0437"},"glyph":{"type":"ImageURL","id":"44a12163faae8c5a53e4ec2b65382a1b"}}},{"type":"ColumnDataSource","id":"5e9edaf02e21b264a83573baf68db2b4","attributes":{"id":"5e9edaf02e21b264a83573baf68db2b4","tags":[],"doc":null,"column_names":["x","y"],"selected":[],"discrete_ranges":{},"cont_ranges":{},"data":{"x":[-6.5791,-48.0915,10.5421,-13.9605,130.0355,40.9705,66.6573,22.7429,-36.3764,46.8825,62.1225,-71.5833,97.924,37.454,39.067,29.1971,63.4666,-24.0024,44.2228,-15.4788,-36.3169,11.1382,-50.4112,-75.9698,58.5948,-50.8181,-26.2748,60.9853,102.5084,41.0679,9.2234,8.9335,-15.8688,-26.9796,-67.5115,48.3267,-14.8296,-34.2108,7.0889,-16.9719,67.4582,45.9508,-51.4178,25.5661,22.3239,21.522,21.9694,75.1961,-45.9737,68.4178,2.3443,-6.3547,-63.4984,23.844,7.4775,-19.8149,-99.2499,-108.2275,1.4429,-61.9877,-7.4362,-12.3282,-6.7674,47.9887,30.0631,-10.8438,-30.7631,5.833,20.3953,-27.2407,-60.3915,84.5491,24.8493,29.0126,-55.1957,7.6946,-3.8091,-7.473,2.3182,105.5828,-21.9541,48.0938,-6.4408,-134.8042,-9.6609,1.0637,-62.2952,-15.0755,-49.8443,-42.1202,0.1267,33.9743,-5.3956,39.1589,52.9943,-15.2577,49.9651,-20.838,0.6866,66.3566],"y":[209.565,216.7215,204.4762,252.8685,117.9589,243.5908,251.3141,96.8614,226.1477,241.751,209.3652,166.7105,231.8517,179.3172,210.0096,224.1032,209.9389,214.968,263.7801,281.0733,198.9704,182.5156,270.1282,171.3691,136.4724,208.5965,153.2761,222.0045,154.3987,212.2252,175.9908,171.4063,148.8949,225.028,239.2234,98.2165,284.4395,228.6698,117.8991,226.2886,240.0292,197.5142,212.794,250.7903,142.793,246.3428,251.482,196.5269,245.5192,268.7308,172.0931,185.1242,156.9801,227.3591,117.203,186.5378,121.8431,294.1036,216.8886,199.7172,195.4862,195.4393,154.9139,235.5677,192.4331,211.599,129.1684,224.4634,271.5054,213.0945,206.359,289.4732,200.9214,181.9924,308.1463,202.3412,161.9291,159.851,243.0936,265.8307,222.0073,194.1204,261.6599,244.2708,154.411,115.796,155.3922,86.2494,143.2853,179.1295,203.0269,194.7406,139.2324,142.0588,277.5106,211.3126,196.8252,147.1948,198.8653,121.4704]}}},{"type":"Circle","id":"07b4cf04fc46a565b348fdbbbc09d467","attributes":{"id":"07b4cf04fc46a565b348fdbbbc09d467","tags":[],"doc":null,"size":{"units":"screen","value":10},"line_color":{"units":"data","value":"black"},"fill_color":{"units":"data","value":"black"},"line_alpha":{"units":"data","value":1},"fill_alpha":{"units":"data","value":0.5},"x":{"units":"data","field":"x"},"y":{"units":"data","field":"y"}}},{"type":"Circle","id":"08e86e8f27ebbe4fd96e1a1a11c07d93","attributes":{"id":"08e86e8f27ebbe4fd96e1a1a11c07d93","tags":[],"doc":null,"size":{"units":"screen","value":10},"line_color":{"units":"data","value":"#e1e1e1"},"fill_color":{"units":"data","value":"#e1e1e1"},"line_alpha":{"units":"data","value":1},"fill_alpha":{"units":"data","value":0.5},"x":{"units":"data","field":"x"},"y":{"units":"data","field":"y"}}},{"type":"GlyphRenderer","id":"a1f75ced3f0cf6dc7d6158af0eb2858c","attributes":{"id":"a1f75ced3f0cf6dc7d6158af0eb2858c","tags":[],"doc":null,"selection_glyph":null,"nonselection_glyph":{"type":"Circle","id":"08e86e8f27ebbe4fd96e1a1a11c07d93"},"server_data_source":null,"name":null,"data_source":{"type":"ColumnDataSource","id":"5e9edaf02e21b264a83573baf68db2b4"},"glyph":{"type":"Circle","id":"07b4cf04fc46a565b348fdbbbc09d467"}}},{"type":"Range1d","id":"826cb861f0509ffbaee501d99d2aa290","attributes":{"id":"826cb861f0509ffbaee501d99d2aa290","tags":[],"doc":null,"start":-300,"end":300}},{"type":"Range1d","id":"b8360254b37f3595a1d243d2edfe3564","attributes":{"id":"b8360254b37f3595a1d243d2edfe3564","tags":[],"doc":null,"start":-20,"end":420}},{"type":"LinearAxis","id":"41a02c7a5a7850d9c2079010a416f695","attributes":{"id":"41a02c7a5a7850d9c2079010a416f695","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"axis_label":"","formatter":{"type":"BasicTickFormatter","id":"0810fd49d189d11e6534bc08963f0861"},"ticker":{"type":"BasicTicker","id":"0001901836e90e73eb41f76558f04368"},"visible":true,"axis_label_text_font_size":"12pt"}},{"type":"BasicTickFormatter","id":"0810fd49d189d11e6534bc08963f0861","attributes":{"id":"0810fd49d189d11e6534bc08963f0861","tags":[],"doc":null}},{"type":"BasicTicker","id":"0001901836e90e73eb41f76558f04368","attributes":{"id":"0001901836e90e73eb41f76558f04368","tags":[],"doc":null,"num_minor_ticks":5}},{"type":"Grid","id":"16e40bc110844322e83a4ee8bc832566","attributes":{"id":"16e40bc110844322e83a4ee8bc832566","tags":[],"doc":null,"dimension":0,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"ticker":{"type":"BasicTicker","id":"0001901836e90e73eb41f76558f04368"}}},{"type":"LinearAxis","id":"7b5d7f1ec855f57c4ae6c4107bcc962f","attributes":{"id":"7b5d7f1ec855f57c4ae6c4107bcc962f","tags":[],"doc":null,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"axis_label":"","formatter":{"type":"BasicTickFormatter","id":"3f9b7d7edfd6ace5358ca3aeef7bd02a"},"ticker":{"type":"BasicTicker","id":"a72d4aa455fa65d4d7e60766572b9a4a"},"visible":true,"axis_label_text_font_size":"12pt"}},{"type":"BasicTickFormatter","id":"3f9b7d7edfd6ace5358ca3aeef7bd02a","attributes":{"id":"3f9b7d7edfd6ace5358ca3aeef7bd02a","tags":[],"doc":null}},{"type":"BasicTicker","id":"a72d4aa455fa65d4d7e60766572b9a4a","attributes":{"id":"a72d4aa455fa65d4d7e60766572b9a4a","tags":[],"doc":null,"num_minor_ticks":5}},{"type":"Grid","id":"7bb8638bb976be601c65154ae6f213ad","attributes":{"id":"7bb8638bb976be601c65154ae6f213ad","tags":[],"doc":null,"dimension":1,"plot":{"type":"Plot","id":"e19bb818e355851e187e0e8a5d606981","subtype":"Figure"},"ticker":{"type":"BasicTicker","id":"a72d4aa455fa65d4d7e60766572b9a4a"}}}];
Bokeh.load_models(all_models);
var model = Bokeh.Collections(modeltype).get(modelid);
var view = new model.default_view({model: model, el: '#9e85f6332c1d2ed3773b91462d76a3aa'});
Bokeh.index[modelid] = view;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment