14414 lines
1.0 MiB
Plaintext
14414 lines
1.0 MiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"This file contains a Matplotlib implementation of the examples provided in the `Examples.ipynb` notebook, for easy comparison."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Let's go!\n",
|
||
|
"\n",
|
||
|
"First import the required modules."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"%matplotlib notebook"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"The autoreload extension is already loaded. To reload it, use:\n",
|
||
|
" %reload_ext autoreload\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Also import numpy as it will be useful…\n",
|
||
|
"import numpy as np\n",
|
||
|
"\n",
|
||
|
"# and add some black magic for easy reloading of the module before executing any cell (just here to ease testing)\n",
|
||
|
"%load_ext autoreload\n",
|
||
|
"%autoreload 2\n",
|
||
|
"\n",
|
||
|
"# ignore matplotlib warnings about too many figures in the notebook\n",
|
||
|
"import matplotlib as mpl\n",
|
||
|
"mpl.rcParams[\"figure.max_open_warning\"] = 50\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Basic plotting\n",
|
||
|
"\n",
|
||
|
"Let's start by doing some basic plotting."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3dcXBV+V338c/W2rWAKamhDS5RiS6ooB1YNazOwtghO45AHRc6Tg1jp6XojJdUU5btH1LaZwboOGXZPxS0HaAzDmSeGWG1ApoadB6yOsIoWUeiY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RfQlAAMB3GBy0W1rs9evtgYHU06AYAhDz0S7ppqR3FPkaAQgAeFN3t93QYO/ebU9Opp4GcyEAMR//IunwHF8jAAEAnpmxDx60Fy+2T51KPQ0ehADEg7xf0rSkH5zj6wQgANS4sTF7yxa7udnu7U09DeaDAMSD/LGk8/f5OgEIADWst9deudLeti2EIKoDAYj7WS7pjqRfuM8+dZKcy+Xc0dHhjo4Od3V1pX5eAwAWwMmT4a4ehw6FU8CobF1dXW++VudyOQIQc/qMpK8+YB/eAQSAGnP7tr1rV7jYo7s79TR4FLwDiLl8l6T/lPT8A/YjAAGghty4Ya9bZ2/YYA8NpZ4Gj4oAxFyek3Rb0rsfsB8BCAA14sKFsLBze3tY6BnViwBELAIQADIun7f37w+3dOvsTD0NSoEARCwCEAAy7OZNe/Nme/Vqu68v9TQoFQIQsQhAAMioK1fspiZ7xw6bb/PZQgAiFgEIABlTKNjHjoUlXo4eDY+RLQQgYhGAAJAhExN2W5u9fLnd05N6GpQLAYhYBCAAZER/v71mjb1pkz0yknoalBMBiFgEIABkwLlzdl2dvW+fPT2dehqUGwGIWAQgAFSx6Wn7+e
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Let's plot a basic data series\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = range(10)\n",
|
||
|
"ax.plot(x)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3deZhWdf3/8ZdkLuwIuIAbLuGC4JJ9xa1y6ZtmueW3XIJv6lXZKDSa+89QMZcssyhLQ61c4GvuaAwuGeRW7uKGuMUIKCIy7MvM/fr98Rl0GmYQ+Nwzn/uc83xc17mcue8zN+9zOcw8OfdZJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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A4cyfb++wg/3//l/qSdYOAYhYBCAAoFBKJfu44+wvf9mur089zdohABGLAAQAFMrvfmdvuqk9c2bqSdYeAYhYBCAAoDCeecbu2NH++99TTxKHAEQsAhAAUAgffWT362dfemnqSeIRgIhFAAIAcq9Uso84wj70ULuhIfU08QhAxCIAAQC594tf2Ftuac+enXqS8iAAEYsABADk2mOPheP+nnwy9STlQwAiFgEIAMitDz6wN9/cvvrq1JOUFwGIWAQgACCXGhrs//5v++ijwzGAeUIAIhYBCADIpYsusrfbzp47N/Uk5UcAIhYBCADInTFj7C5d7BdeSD1J2yAAEYsABADkyqOPhpM+/vrX1JO0HQIQsQhAAEBuTJ1q9+xpX3
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Or a (X, Y) point series\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = range(10)\n",
|
||
|
"y = [i**2 for i in x]\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAY6ElEQVR4nO3dX4zlaV3n8c/MUTjAMBAujGHAbJWbOJFhJhoJW5q4aQN7RdhoYjaG7Ew2pC7WwpmUETnZhKABmRgN14Zg9kr43RjupLiQyEQvjBqNeuNOZJGe4a8L9lConDj97MWvhilruhq6v9V96lv1eiVPuuqcX508+fWpOu9+6vmdTgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAviniQPJLnfMAzDMIxW44HMr+Nwyx5IMgzDMAzDaDkeCNyG+5OMq1evjmvXrhnFsbe3t/E5XIThPDqX53E4l87jeRpXr159IQDv33BH0NT9Sca1a9cGdfv7+5uewoXgPJ4d5/LsOJdnw3k8G9euXROAlAjAM+QH29lwHs+Oc3l2nMuz4TyeDQFIlQA8QwcHB5uewoXgPJ4d5/LsOJdnw3k8GwKQKgEIAM0IQKoEIAA0IwCpEoAA0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/Tf5WgEIAM0IQKoEIAA0IwCpEoAA0IwApEoAAkAzApAqAQgAzQhAqgQgADQjAKkSgADQjACkSgACQDMCkCoBCADNCECqBCDATazX6zFN01itVmOaprFerzc9JRCAlAlAgFOs1+uxs3NlLJfbY7HYHcvl9tjZuSIC2TgBSJ
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANS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AKkSgADQjACkSgACQDMCkCoBCADNCECqBCAANCMAqRKAANCMAKRKAAJAMwKQKgEIAM0IQKoEIAA0IwCpEoAA0IwApEoAAkAzApAqAQgAzQhAqgQgADQjAKkSgADQjACkSgACQDMCkCoBCADNCECqBCAANCMAqRKAANCMAKRKAAJAMwKQKgEIAM0IQKoEIAA0IwCpEoAA0IwApEoAAkAzApAqAQgAzQhAqgQgADQjAKkSgADQjADkk0muJ/mZY7c9nOSzSQ6TPJPkAzf5egEIAM0IwMvt0SQHSZ7PiwF4X5IvJvlQkpcleSjJ1SRPnPIYAhAAmhGAl9cbknz+6M/jK4CPJflyknuPHft4kqdPeRwBCADNCMDL69NJ3n308fEA/EiST504difzKuF9N3gcAQgAzQjAy+kXMwfgC44H4MeSfOLE8Q9mDsDX3+CxBCAANCMAL5/tzHv83njstvIK4N7e3tjf3x/7+/vj4OBg089rAOCEg4OD77xW7+3tCcBL5rEk/5rkq0m+djSuJ/lGkt/JfGHIV/Lv9wA+EXsAAeDCsAJ4+Swz/yr3+Lie5OeTvDbzKt+zST54dOybk3whrgIGgAtDAJL8+7eBSea3fnkqybcy/7r4/Tf5WgEIAM0IQKoEIAA0IwCpEoAA0IwApEoAAkAzApAqAQgAzQhAqgQgADQjAKkSgADQjACkSgACQDMCkCoBCADNCECqBCDATazX6zFN01itVmOaprFerzc9JRCAlAlAgFOs1+uxs3NlLJfbY7HYHcvl9tjZuSIC2TgBSJ
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Plotting a discrete (X, Y) point series\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = range(10)\n",
|
||
|
"y = [i**2 for i in x]\n",
|
||
|
"ax.scatter(x, y)\n",
|
||
|
"fig.show()\n",
|
||
|
" \n",
|
||
|
"# or equivalently plotting a discrete (X, Y) point series\n",
|
||
|
"fig2, ax2 = plt.subplots()\n",
|
||
|
"x = range(10)\n",
|
||
|
"y = [i**2 for i in x]\n",
|
||
|
"ax2.scatter(x, y)\n",
|
||
|
"fig2.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 17,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO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kcBGA5fF3ZK3iV/qP660WSpkr6SNIjt52/VNJfKbsO4GVJ35P0y4N8bwIQsH3hgj1hgv3aa6knARBbdb937kw9STwEIEIRgCi9nh57wQL7mWdSTwIgtqLuNwGIUAQgSq+lxW5qsru7U08CILaWFnvu3OLtNwGIUAQgSm3PHrux0e7oSD0JgNiKvN8EIEIRgCit06fthgb74MHUkwCIrej7TQAiFAGIUurqsmfOtF98MfUkAGKr7vemTaknqR0CEKEIQJROX5+9YoW9dKnd25t6GgAxlWW/CUCEIgBROm1t9pQpdmdn6kkAxNbWZk+dWvz9JgARigBEqRw9ml0M9s03U08CILYy7TcBiFAEIErj0iV70iR7+/bUkwCIrWz7TQAiFAGIUrh50160yF69OnuPEIDiqO73mjXl2W8CEKEIQJTCxo32nDn29eupJwEQWxn3mwBEKAIQhbd/vz12rP3uu6knARBbWfebAEQoAhCFdvZs9uSwf3/qSQDEVt3v119PPcnoIwARigBEYV2/bs+ebT//fOpJAMRW9v0mABGKAEQh9fXZq1bZixfbt26lngZATOw3AYhwBCAK6dVX7cmTs0tDACgW9psARDgCEIVz/Hh2Mdhjx1JPAiA29jtDACIUAYhC6ezMPuatrS31JABiq+73K6+kniQ9AhChCEAURm+vvWSJ/fTT5bkYLFAW1f1euZ
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Plotting broken lines\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = [1, 2, 3, 2, 1]\n",
|
||
|
"y = [1, 1, 2, 2, 1]\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 22,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3deXhU5dnH8bvVqiQQNkFeUETrhrJIteLSinsvLSBiXeqGvrVFjdKmoqJtxaVqW9Fq1VbF1tYFUXGrW2oVUXFfXlEUwQVpwg7KsCSQZX7vHw/YEJOQyTMzz5xzvp/rOpcwnIT7eAL5MmczAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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Wjj9eGjLEXf0LFBoCEL4IQABo5Oqrpd69ecYvChcBCF8EIAA08MQTUnGx9M47oScBmkcAwhcBCADrzZkjdewoTZoUehKgZQQgfBGAACB30Ue/ftIvfxl6EmDTCED4IgABJF46LZ14onTQQVJtbehpgE0jAOGLAASQeH/8o9Srl7R4cehJgNYhAOGLAASQaNOmuSd9vPpq6EmA1iMA4YsABJBYlZVS9+7SX/4SehIgMwQgfBGAABJp3Tppv/2k00935wACUUIAwhcBCCCRfvELac89paqq0JMAmSMA4YsABJA4Dz7o7vf3ySehJwHahgCELwIQQKLMni2VlEiPPBJ6EqDtCED4IgABJMaaNVL//tLYsaEnAfwQgPBFAAJIhHRaGjVK+t73pJqa0NMAfgjA5LjczOab2Sozm2Zme7Sw7jQzW2dmK9evv9LMzm
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Plotting a function\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(-2, 2, 100)\n",
|
||
|
"y = np.sin(x)\n",
|
||
|
"ax.plot(x, y) # Plot sinus on [-2, 2]\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 23,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nOzdd3gVZf428PEFIT2BUASUIkKwIBZQQFFQUETYVRREVEBwVxcWJbr+dHUBhYQqRQFBAcUC0pRIjUo7J72HkAaBdEJ6O6mnzP3+MQmknJoJTMr9ua5zrZtMhm9y5szc88xTBIGIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIqNYhQRBEQRCeUroQIiIiIrrxZgmC4CsIgkFgACQiIiJq824XBCG15n/Z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dvgzceitQVKR0JUTNiwGQ5GIAJKI2a80aYOJEpasgan4MgCQXAyARtVkjRwLffqt0FUTNjwGQ5GIAJKI2KSMD6NgRyM1VuhKi5scASHIxABJRm/TVV8DYsUpXQXRjMACSXAyARNQmjR0rhUCitogBkORiACSiNic3V3r8m5mpdCVENwYDIMnFAEhEbc6330oDQIjaKgZAkosBkIjanIkTpSlgiNoqBkCSiwGQiNqUoiJp8ufLl5
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# And we can plot multiples graphs on the same figure\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"ax.plot(range(5))\n",
|
||
|
"for i in range(8):\n",
|
||
|
" x = np.linspace(-10, 10, 100)\n",
|
||
|
" y = [np.sin(abscissa + np.pi * i / 4) for abscissa in x]\n",
|
||
|
" ax.plot(x, y)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Advanced plotting\n",
|
||
|
"\n",
|
||
|
"We can do more elaborated stuff."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 25,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3df5yVd33n/XcSaxRwAhEUDNQOa6AVXB8QlYk1UL2ZaAXSNeDdpsMaFbG7DtidEuK2Irr7ALxbCdltC1UbyL0KWF1IdYE67kS7TGyFWwErU3VUJpZRJgE7ZMjIBGbmvO8/vmfC5HDmB1znzPec67yej8d5MOeca+b6XDAwL67rXNeRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADl668lZSQ9dA2fs0jSx/M8/urs13rPoMful/S+PMu+N7vsr17DeoczTtLnJT2d/brbCvR18xlqm+7PrntmEdctSbMlfUNSl6R+Sfco/Hn0F3m9AAAgBV4i6RmFcOiQdOMoP28gNnKXf7GkN0l6+aDH/l5Sc56vcX/2ax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bY8+fbbW2xpxkeAYikCEAAQMVrarInT7ZXr7Z7emJPMzICEEkRgACAitXfb2/aFC7xsmtX7GlGjwBEUgQgAKAidXbaS5bYM2fax4/HnubaEIBIigAEAFScY8fs6mp72bIQguWGAMRQXiFpr6SnJHVK+gdJC/MsRwACACrKI4+EQ76bN4dDwOWIAMRQ9ks6LOlWSTdI+iNJFyRNzFmOAAQAVISLF+33vz+c7NHUFHuaZAhADOW7ktYOuj9eUkbSG3OWIwABAKl36p
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Plot with a label on the axis, and a title for the figure\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = range(10)\n",
|
||
|
"ax.plot(x, x, label=\"x\")\n",
|
||
|
"ax.set_xlabel(\"some x label\")\n",
|
||
|
"ax.set_ylabel(\"some y label\")\n",
|
||
|
"ax.set_title(\"A title for the figure\")\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 28,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3dd3xUVfrH8efn7toXwbqKouuq2EBXXcG1IQg2sLvuShcsa1QIYMWGWBbEgrq2FXXtuwgWVIJIsSIWFFRAKYqRXkNoCcl8f38cZsWYhCR3Zs4tn/frdV/KMJn5ktzc+8y95zzHDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBEnW9m75pZkZmVm9lmm3h+fTN7zsxWmNkyM3vGzLbLZkAAAABkVmtzRWBXq1kB+IaZvWVmDcxsezMbbWavZDMgAAAAsuN423QB2MjMUmZ28EaPNd3w2O7ZiwYAAIBsqEkBeLqZrank8XVm1jYboQAAAJA9NSkAO5jZ/EoeX2BmF1Ty+P+ZWUMzq8fGxsbGxsYWqa2hufM4Yi4bVwAbmpnY2Nj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4yo5vlAAIigKwI28+660ww5SaanvJHXTq1c0r14m1eDB0ZmNWZkWLaT77/edAjXVubPUu7fvFHVTWuqaQr/3nu8k4UEBmBz9zGyumRWb2XgzO6ia5443sxIzW7
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"ax.set_xlabel(\"some x label\")\n",
|
||
|
"ax.set_ylabel(\"some y label\")\n",
|
||
|
"ax.set_title(\"A title for the figure\")\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.sin(x)\n",
|
||
|
"ax.plot(x, y, label=\"sin\")\n",
|
||
|
"ax.legend(\"lower right\") # Not that ax.legend call position matters\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 30,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3dd5hU1f3H8Y+Q2AXFhtiNomKL/lTsqLHHEo3G3iuuoptYUYMl2FvEWGIMUbBrjLGtKGo0tohiCyIaihApIrC0BXZ3vr8/zkyYHWZnZ/femXPPzPv1PPfZ3Zk7937nsux+9txTJAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAuB6QlJJ0Wzte00fSgDyPr58+1klZj50s6dQ8+56S3ne9dpy3kOUlDZE0NX3c22M6bj6tvaeT0+feqITnlqRNJb0uqV5Ss6RD5f49mkt8XgAAUAGWlTRLLjhMltSpyNdlwkbu/ktL2lHSqlmPvSHprTzHODl9jLgC4MWSFkk6Pl3DujEdN5+23lOpA+BLksZK2lfuvXaV1CP9OQAAQEHHyrVYPS8XXA4q8nVXp/fvXMS+5QqAgyWNj+lYkguzrfEdAMdK+kuJz1GsQtcJAAAkUJ2k6XItdvMkPVHEawbIhcbm9MfM59KSt4DfyLPv6+nnWguAZ0n6RFKDpO8l/UnSKm3UlK+ePdLP9ZT0rKSZkuZLek/S/jmvvzr9ui3krsmc9GvyKeY99ZY0VO4W7X8l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AAQAIDAEQE
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"ax.set_xlabel(\"some x label\")\n",
|
||
|
"ax.set_ylabel(\"some y label\")\n",
|
||
|
"ax.set_title(\"A title for the figure\")\n",
|
||
|
"ax.set_xlim(-20, 10)\n",
|
||
|
"ax.set_ylim(-3, 3)\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.sin(x)\n",
|
||
|
"ax.plot(x, y, label=\"sin\")\n",
|
||
|
"ax.legend(\"lower right\") # Not that ax.legend call position matters\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 31,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3dd5xU1f3/8fcuTQGBYAHBSjB2f5FYsBu+ghoLGjVGjaKxREUl+/3aK/ZYknw1idFoxF7TjG1taOw19hYrIhZAOqyU3fP748x+GYaZ2dm59zPnnuX1fDzOgy2zez/n3HXu21vOkQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8bpWUoukX7fjZ3aQdE6Rr6+Z+12H5H1tlKTDirz20Nxr12jHdsvpLulmSV/nfu9vUvq9xZTq06jctgcZbluS1pU0XtJMSc2S9pTfH83G2wUAAB3AcpJmyAeHLyXVV/hzrWGj8PVdJW0hacW8rz0u6ckiv2NU7nekFQBPkrRA0kG5GlZP6fcW01afrAPgA5I+ljRcvq+9JQ3IfQwAAFDWAfJnrO6VDy4/qvDnxuZe36mC19YqAI6T9GlKv0vyYbaU0AHwY0k3GG+jUuXGCQAAZFCjpKnyZ+zmSrqzgp85Rz40Nuf+bf1YWvoS8ONFXjs+971SAfAoSa9JapI0RdJ1kr7TRk3F6tk+973vSfq7pOmS5kl6TtLOBT8/NvdzG8qPyezczxRTSZ+2lHSL/CXaSZKu0NJBaXlJl8iHufm5f0+XVFemnzuU6Gt+H/KtJOn2XB3TJP1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
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Passing extra arguments to matplotlib plot command\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"ax.set_xlabel(\"some x label\")\n",
|
||
|
"ax.set_ylabel(\"some y label\")\n",
|
||
|
"ax.set_title(\"A title for the figure\")\n",
|
||
|
"ax.set_xlim(-20, 10)\n",
|
||
|
"ax.set_ylim(-3, 3)\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.sin(x)\n",
|
||
|
"ax.plot(x, y, label=\"sin\", linewidth=20)\n",
|
||
|
"ax.legend(\"lower right\") # Not that ax.legend call position matters\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 42,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": true
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nOzdeVhUZf8G8Mdsf9+s3urt/RXumqapaZrlnpVL2aJmi5pamWuLlqZlNSAoorijuKO44IK7IG7gggug4or7giDuCKjszP37gygXkJk558wz55z7c11cV+HMOfdVU9zMPM/3EYKIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIh06jMhxFYhRKoQIk8I8UAxj39KCDFfCJEihEgWQswVQjypZUAiIiIiUte7Ir8EfiVsK4AhQoj1QoinhRD/EUJsEEKs0DIgEREREWmjqSi+AJYRQliFEK/c9r2af33PTbtoRERERKQFWwrgh0KI9EK+nymEaKNFKCIiIiLSji0FsLMQ4kIh378ohOhYyPdLCCFeFEKU4he/+MUvfvGLX7r6elHk/xwng9PiHcAXhRDgF7/4xS9+8Ytfuvx6UZDhNRW2rQHME3euAaz11/cKWwNYSgiBhIQEpKam8ssEX3379v37rxPPX0bd1hPQqu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z8VB5D5w6e81p9xzis9Fl5wGyAJ
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"import seaborn.apionly as sns\n",
|
||
|
"# You can also tweak the palette used, either with a seaborn palette\n",
|
||
|
"sns.set_palette(sns.color_palette(\"dark\", 10))\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.sin(x)\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"fig.show()\n",
|
||
|
"sns.reset_orig()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 37,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3deXSU9dn/8as+v3NaAoQdVBYFiygg1rqhqLgg7loeLVpcqGvVqDVWq7Vqhapo5VDXR6V1KSpSbRVLsREXkKKIFkRxRUH2VSAJAUKSmc/vj2+ogJMQ8p2Z79xzv1/n3EcyuZNc2Gnn3clc95gBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgP4wws4/MrMzMlprZWDPrtIOvmWJmm82s3MzW1/7z8syNCAAAgHS608wOMLP/Z2aFZvasmX2wg6+ZbGbDMjwXAAAAsmR/M0uYWYt6zplsZsOzMw4AAAAy7ddmNn8H50w2s9VmtsbMPjX3a+SmGZ4LAAAAGTDA3Gv6jt/BeX3NrGXtn/czs1lm9lwG5wIAAEAGnGpm68zs9EZ87VFmVmVm30/xue+ZWUdzry/k4ODg4ODgiM7R0dzjOPLUuebib0Ajv35LAP4gxec6mpk4ODg4ODg4Inl0NOSlq8xsrZn1a+D57c3sBDMrqP24l5m9b2Yv1HF+oZlp8eLFKisr4+BIy1FUVBR8hnw+4vrvN+p/71yePxdmCzFDNn5mpn7G4sWLtwRgYeMTA7ksad9e02/r6/ptCcLOtbdt+biLmc0w94xhuZnNtfqXQArNTGV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ACEACQs+bOlQ4+WNpvP2nOnNDT5A4CEL4IQABAzkkmpccfl5o1k375S2nTptAT5RYCEL4IQABATlm7VjrrLKlDB+lf/wo9TW4iAOGLAAQA5IzJk6VOnaRTTpFWrgw9Te4iAOGLAAQABLd5s3TTTVLTptLDD7PosSMEIHwRgACAoL74QjroIGn//aVPPgk9TTQQgPBFAAIAgkgmpT//2S16XHedVFkZeqLoIADhiwAEAGTdN99IZ54p7bqr9OqroaeJHgIQvghAAEBWvfGG1LGjdNpp0qpVoaeJJgIQvghAAEBWbN4s3XCDW/R45BEWPXwQgPBFAAIAMu7zz6Uf/1j60Y+kTz8NPU30EYDwRQACADImmZRGj3bP+v3qVyx6pAsBCF8EIAAgI775Rho0SNptN+m110JPk18IQPgiAAEAaff669Luu0s/+YkLQaQXAQhfBCAAIG0qK6Xrr3e/8n3sMRY9MoUAhC8CEACQFp99Jh1wgFv2+Pzz0NPkNwIQvghAAICXZNJd1qVpU3eZl82bQ0+U/whA+CIAAQCNtnq1dMYZ7sLOb7wRepr4IADhiwAEADTKpEnurdz+939Z9Mg2AhC+CEAAwE6prJSuu05q1kz6859Z9AiBAIQvAhAA0GCffCL16SMddJD0xRehp4kvAhC+CEAAwA4lk9LDD7tFjxtvZNEjNAIQvghAAEC9Vq6UTj1V6tRJmjw59DSQCED4IwABAHUqKZE6dJDOOktasyb0NNiCAIQvAhAA8B2bNknXXusWPZ54gkWPXEMAwhcBCADYxpw50n77SQcfLH35ZehpkAoBCF8EIABAknuW78EHpYIC6eabpaqq0BOhLgQgfBGAAACtWCGdfLLUubP01luhp8GOEIDwRQACQMxNnCi1by8NHiytXRt6GjQEAQhfBCAAxNTGjdLVV0vNm0tPPcWiR5QQgPBFAAJADH30kdS7t3ToodJXX4WeBjuLAIQvAhAAYiSZlO
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# To plot in log scale\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(1, 10, 100)\n",
|
||
|
"y = np.log(x)\n",
|
||
|
"ax.semilogx(x, y)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 34,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoA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8Wd790LPQ0IQPgiAAEAm3rwQDpwQOrulhYWQk8DiQCEPwIQAPBCN25ItbXShQtSPh96GjxFAMIXAQgAeM5nn0nptFRXJ925E3oaPIsAhC8CEADwFbOzUnu79Prr0sxM6GlQDAEIXwQgAOAL4+NSfb106pS0vBx6GrwIAQhfBCAAQGtr0uCgVFMjjYyEngZbIQDhiwAEgCq3uCgdPSo1N0uTk6GnwXYQgPBFAAJAFZuclJqaXAAuLoaeBttFAMIXAQgAVWpkxF3V49IldwgYlYMAhC8CEACqzPKydOKEO9ljfDz0NNgNAhC+CEAAqCIzM9KhQ1JHh1vuBZWJAIQvAhAAqsTt225h5/5+aWUl9DTwQQDiRV4ys6yZLZjZP2xyPwIQABIun5fOn3eXdLt5M/Q0iAIBiM28ZmZ/awQgAFStx4+lw4elAwekqanQ0yAqBCC28rYRgABQlSYmpP37pZ4eif/MJwsBiK0QgABQZQoFaWjILfFy9aq7jWQhAJPrLXOf4cuZ2ZqZvVzkPt8zs9+a2R/M7EMzay1yHwIQAKrI0pLU2yvt2ydls6GnQakQgMnVbS4Cv23FA3DAzH5tZt80s6+b2WUz+42Z7Xnmfm+b2T9u8jwEIAAkxPS01NoqdXZK8/Ohp0EpEYDJ12XFA3DGzE5vuP01M/udmfVu+N6omT0ws4dm9vP1+zyLAASABBgbk1IpaWBAWl0NPQ1KjQBMvmIBmDKzgpm1P3Pfn5nZlR0+fsrMlE6ndebMGZ05c0
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Same for log-log scale\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(0, 10, 100)\n",
|
||
|
"y = x**2\n",
|
||
|
"ax.loglog(x, y)\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 36,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3de5SdVXn48YdrIIEAwQtXQSpKEU0BRcWlFLxQqVVEqUXBqEC9DAInCBVEhaKiSymyvCIqikBFUEGqHBBoxfy0ShUUsCJICeEuAknAEJLM8/tjJwSHmSRkz+Rkn/l81npXLnMg+105zPmy33fvNwIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOgTa0TElhEx2eFwOBwOR1PHllE+x+FJ2zIi0uFwOBwOR5PHlgErYXJE5KxZs3L27Nl9dwwMDPR8DM7NuY2Xc+v383NubR79em6zZs1aEoCTe9wRNGpyROTs2bOzH3U6nV4PYcw4tzb187ll9vf5Obc29eu5zZ49WwBSRQA2yrm1qZ/PLbO/z8+5talfz00AUquvA7Db7fZ6CGPGubWpn88ts7/Pz7m1qV/PTQBSq68DEAD6kQCklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQGoJQABojACklgAEgMYIQFbE9yJiMCL2GuZrAhAAGiMAWZ63RUQ3IhaFAASAviAAWZatIuLWxT+aAQSAPiEAWZZLI+LgxT8XgADQJwQgI3lvlABcQgACQJ8QgAxnu4i4MyK2ftzvLTMABwYGstPpZKfTyW632+v3NQAwRLfbfeyzemBgQADyBNMi4pGIuDci/rj4GIyIByLiS0NeawYQABpjBpDhrBcRWww5BiNi/4jYeMhrBSAANEYAsqJsAwMAfUIAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAUksAAkBjBCC1BCAANEYAMpwPR8TNEfFgRNwbEZdExNQRXisAAaAxApDhbB8RGy3++doRMT0i7o6INYZ5rQAEgMYIQJZnQkQcGRGLImLTYb4uAAGgMQKQkewTEQ9ExGBELIyIT43wOgEIAI0RgCzPxhFxRES8cYSvC0AAaIwAZEWsEWVByPOG+ZoABIDGCEBWxNoR8XBE7DfM1yZHRA4MDGSn08lOp5PdbrfX72sAYIhut/vYZ/XAwIAA5AkOj4inLf75UyPiyxFxf0Q8fZjXmgEEgMaYAWQ4F0fEXRExNyLuiIgLI2KXEV4rAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQABojAKklAAGgMQKQWgIQAFZj8+dnzpyZ+bOfZX7nO5mf+1zmUUcJQOoIQADogYULM++4I/PqqzMvuijzi1/MPP74zHe+M/M1r8mcOjXzqU/NjMhcc83MzTfP3GWXzNe+NnPaNAFIHQEIAKNocDDzgQcyr78+89JLM888M/OjH818z3syX//6zBe8IHOLLUrURZTImzq1RN/BB2d+6EMlBi+6qMThHXdkLljwl3+GS8DUEoAAsIIefTTzttvK5dgLLsj8zGcyjz468y1vydxjj8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il41wtcFIECPDA5m3nNP2Qfv7LPLfXhvf3vmy19eLs2usUbmeutl/vVfZ+6zT+Zhh5XVtt/7Xuavfz32z5tl9SUAWZbXRsQDEfG6ZbxGAAKMoQULyuXWyy4rs3Pvf3+ZxXv+8zM32GDpPni7717uw/vIRzK/8Y3Mn/wk8/bb7YHH8AQgI3lrlPh75XJeNzkicmBgIDudTnY6nex2u71+XwM0Zd688izaiy7K/Ld/y3zve8u9eM96Vllssfba5ed7712+dsopmRdemHnddVbSsuK63e5jn9UDAwMCkCc4LCLuj4iXrsBrzQACrICHHy7PlP3ud8uCi0MPzdxzz8ytty6Xatdfvzybdt99yyzfl75UVtPeckuZBYTRZAaQ4QxGxPyImLP4mLv4x+GCUAACLPbnP5fI+853SuQdckh5bNmWW5ZLtRtskP
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# To force an orthonormal frame, use orthonormal=True on the plot\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(1, 10, 100)\n",
|
||
|
"y = np.log(x)\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"ax.axis(\"equal\")\n",
|
||
|
"fig.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Using subplots"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 45,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nOzdebiOVdsG8KWSkJkMEZlJpjKlzGPJnDEqFGVq4y0iMmTKrDILRUmmItucZMhcSco8DyHzvPf5/bHaX2jv7Xmee6113Xt3/o7D8dX23Ne13rzv95ytda+1lCIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiI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SC3Sp8eWL/e/JiCxQBIREQSfBMAs2UDVq4M/rn33weaNDE+HN+4ckWHnOPHg3+2aVP9zye+2rIFSJ06tGefeAKYPdvseELBAEhERBJ8EQAjIoD77wf++CP4Z+P7bSB79wL33af/GQXrrbeAN94wPya/WLAAKFAgtGdD2T1sAwMgERFJ8EUAPHFCz3JduhT8s4sXA3nymB+TX6xeDWTJEtqzI0b457w7G8aPBypXDu3Z11/XAVkaAyAREUnwRQDcsgVImTK0Z3/+GUiWzOx4/GTmzODPR4zy5ZdA8eJmx+MnvXsDL70U2rP9+uklcmkMgEREJMEXAfCbb0Jfyjt1Ss
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Applying a grid on a figure, one empty subplot\n",
|
||
|
"ax = plt.subplot2grid((1, 2), (0, 0))\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.cos(x)\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 46,
|
||
|
"metadata": {
|
||
|
"collapsed": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nOzdeZxN9f8H8Hf5/r5lbKFFSKWFbJWULZFQooWyViQVGpWxpChrFBUhWbJEKWWJsoxkyxKyJSRbmBnLGGOuYTZz7+v3x9v9NsbcO/fes3zO8n4+HudRc+fcz3m7c+857/s5n8/7QySEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhLCpNkT0KxF5iMhLRFfms/81RDSLiFKIKJmIviKiYkYGKIQQQggh9NWYOAnsRKElgIuJ6GciKk5EJYhoOREtMDJAI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KGHeK1gN4qNjf3ftTo6OloSQBfIawxgIgUeA5ibPwG8Oo/fub4H8MQJoFAhYMMG1ZHoJyNDyiZY1datPHvWSctbxcfzv2nbNtWRiNxmzeIl3+xQZDxU69cDhQsDJ0+qjkQt6QF0h95EdJi49EtB4jF9cZT3LODriejRHL
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)\n",
|
||
|
"ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)\n",
|
||
|
"ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)\n",
|
||
|
"ax4 = plt.subplot2grid((3,3), (2, 0))\n",
|
||
|
"ax5 = plt.subplot2grid((3,3), (2, 1))\n",
|
||
|
"x = np.linspace(-10, 10, 100)\n",
|
||
|
"y = np.cos(x)\n",
|
||
|
"ax1.plot(x, y)\n",
|
||
|
"ax2.plot(x, y)\n",
|
||
|
"ax3.plot(x, y)\n",
|
||
|
"ax4.plot(x, y)\n",
|
||
|
"ax5.plot(x, y)\n",
|
||
|
"plt.show()"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Saving figures"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 39,
|
||
|
"metadata": {
|
||
|
"collapsed": false,
|
||
|
"scrolled": false
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"application/javascript": [
|
||
|
"/* Put everything inside the global mpl namespace */\n",
|
||
|
"window.mpl = {};\n",
|
||
|
"\n",
|
||
|
"mpl.get_websocket_type = function() {\n",
|
||
|
" if (typeof(WebSocket) !== 'undefined') {\n",
|
||
|
" return WebSocket;\n",
|
||
|
" } else if (typeof(MozWebSocket) !== 'undefined') {\n",
|
||
|
" return MozWebSocket;\n",
|
||
|
" } else {\n",
|
||
|
" alert('Your browser does not have WebSocket support.' +\n",
|
||
|
" 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
|
||
|
" 'Firefox 4 and 5 are also supported but you ' +\n",
|
||
|
" 'have to enable WebSockets in about:config.');\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
|
||
|
" this.id = figure_id;\n",
|
||
|
"\n",
|
||
|
" this.ws = websocket;\n",
|
||
|
"\n",
|
||
|
" this.supports_binary = (this.ws.binaryType != undefined);\n",
|
||
|
"\n",
|
||
|
" if (!this.supports_binary) {\n",
|
||
|
" var warnings = document.getElementById(\"mpl-warnings\");\n",
|
||
|
" if (warnings) {\n",
|
||
|
" warnings.style.display = 'block';\n",
|
||
|
" warnings.textContent = (\n",
|
||
|
" \"This browser does not support binary websocket messages. \" +\n",
|
||
|
" \"Performance may be slow.\");\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj = new Image();\n",
|
||
|
"\n",
|
||
|
" this.context = undefined;\n",
|
||
|
" this.message = undefined;\n",
|
||
|
" this.canvas = undefined;\n",
|
||
|
" this.rubberband_canvas = undefined;\n",
|
||
|
" this.rubberband_context = undefined;\n",
|
||
|
" this.format_dropdown = undefined;\n",
|
||
|
"\n",
|
||
|
" this.image_mode = 'full';\n",
|
||
|
"\n",
|
||
|
" this.root = $('<div/>');\n",
|
||
|
" this._root_extra_style(this.root)\n",
|
||
|
" this.root.attr('style', 'display: inline-block');\n",
|
||
|
"\n",
|
||
|
" $(parent_element).append(this.root);\n",
|
||
|
"\n",
|
||
|
" this._init_header(this);\n",
|
||
|
" this._init_canvas(this);\n",
|
||
|
" this._init_toolbar(this);\n",
|
||
|
"\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" this.waiting = false;\n",
|
||
|
"\n",
|
||
|
" this.ws.onopen = function () {\n",
|
||
|
" fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
|
||
|
" fig.send_message(\"send_image_mode\", {});\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onload = function() {\n",
|
||
|
" if (fig.image_mode == 'full') {\n",
|
||
|
" // Full images could contain transparency (where diff images\n",
|
||
|
" // almost always do), so we need to clear the canvas so that\n",
|
||
|
" // there is no ghosting.\n",
|
||
|
" fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
" }\n",
|
||
|
" fig.context.drawImage(fig.imageObj, 0, 0);\n",
|
||
|
" };\n",
|
||
|
"\n",
|
||
|
" this.imageObj.onunload = function() {\n",
|
||
|
" this.ws.close();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" this.ws.onmessage = this._make_on_message_function(this);\n",
|
||
|
"\n",
|
||
|
" this.ondownload = ondownload;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_header = function() {\n",
|
||
|
" var titlebar = $(\n",
|
||
|
" '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
|
||
|
" 'ui-helper-clearfix\"/>');\n",
|
||
|
" var titletext = $(\n",
|
||
|
" '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
|
||
|
" 'text-align: center; padding: 3px;\"/>');\n",
|
||
|
" titlebar.append(titletext)\n",
|
||
|
" this.root.append(titlebar);\n",
|
||
|
" this.header = titletext[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_canvas = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var canvas_div = $('<div/>');\n",
|
||
|
"\n",
|
||
|
" canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
|
||
|
"\n",
|
||
|
" function canvas_keyboard_event(event) {\n",
|
||
|
" return fig.key_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" canvas_div.keydown('key_press', canvas_keyboard_event);\n",
|
||
|
" canvas_div.keyup('key_release', canvas_keyboard_event);\n",
|
||
|
" this.canvas_div = canvas_div\n",
|
||
|
" this._canvas_extra_style(canvas_div)\n",
|
||
|
" this.root.append(canvas_div);\n",
|
||
|
"\n",
|
||
|
" var canvas = $('<canvas/>');\n",
|
||
|
" canvas.addClass('mpl-canvas');\n",
|
||
|
" canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
|
||
|
"\n",
|
||
|
" this.canvas = canvas[0];\n",
|
||
|
" this.context = canvas[0].getContext(\"2d\");\n",
|
||
|
"\n",
|
||
|
" var rubberband = $('<canvas/>');\n",
|
||
|
" rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
|
||
|
"\n",
|
||
|
" var pass_mouse_events = true;\n",
|
||
|
"\n",
|
||
|
" canvas_div.resizable({\n",
|
||
|
" start: function(event, ui) {\n",
|
||
|
" pass_mouse_events = false;\n",
|
||
|
" },\n",
|
||
|
" resize: function(event, ui) {\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" stop: function(event, ui) {\n",
|
||
|
" pass_mouse_events = true;\n",
|
||
|
" fig.request_resize(ui.size.width, ui.size.height);\n",
|
||
|
" },\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function mouse_event_fn(event) {\n",
|
||
|
" if (pass_mouse_events)\n",
|
||
|
" return fig.mouse_event(event, event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" rubberband.mousedown('button_press', mouse_event_fn);\n",
|
||
|
" rubberband.mouseup('button_release', mouse_event_fn);\n",
|
||
|
" // Throttle sequential mouse events to 1 every 20ms.\n",
|
||
|
" rubberband.mousemove('motion_notify', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
|
||
|
" rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
|
||
|
"\n",
|
||
|
" canvas_div.on(\"wheel\", function (event) {\n",
|
||
|
" event = event.originalEvent;\n",
|
||
|
" event['data'] = 'scroll'\n",
|
||
|
" if (event.deltaY < 0) {\n",
|
||
|
" event.step = 1;\n",
|
||
|
" } else {\n",
|
||
|
" event.step = -1;\n",
|
||
|
" }\n",
|
||
|
" mouse_event_fn(event);\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" canvas_div.append(canvas);\n",
|
||
|
" canvas_div.append(rubberband);\n",
|
||
|
"\n",
|
||
|
" this.rubberband = rubberband;\n",
|
||
|
" this.rubberband_canvas = rubberband[0];\n",
|
||
|
" this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
|
||
|
" this.rubberband_context.strokeStyle = \"#000000\";\n",
|
||
|
"\n",
|
||
|
" this._resize_canvas = function(width, height) {\n",
|
||
|
" // Keep the size of the canvas, canvas container, and rubber band\n",
|
||
|
" // canvas in synch.\n",
|
||
|
" canvas_div.css('width', width)\n",
|
||
|
" canvas_div.css('height', height)\n",
|
||
|
"\n",
|
||
|
" canvas.attr('width', width);\n",
|
||
|
" canvas.attr('height', height);\n",
|
||
|
"\n",
|
||
|
" rubberband.attr('width', width);\n",
|
||
|
" rubberband.attr('height', height);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Set the figure to an initial 600x600px, this will subsequently be updated\n",
|
||
|
" // upon first draw.\n",
|
||
|
" this._resize_canvas(600, 600);\n",
|
||
|
"\n",
|
||
|
" // Disable right mouse context menu.\n",
|
||
|
" $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
|
||
|
" return false;\n",
|
||
|
" });\n",
|
||
|
"\n",
|
||
|
" function set_focus () {\n",
|
||
|
" canvas.focus();\n",
|
||
|
" canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" window.setTimeout(set_focus, 100);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items) {\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) {\n",
|
||
|
" // put a spacer in here.\n",
|
||
|
" continue;\n",
|
||
|
" }\n",
|
||
|
" var button = $('<button/>');\n",
|
||
|
" button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
|
||
|
" 'ui-button-icon-only');\n",
|
||
|
" button.attr('role', 'button');\n",
|
||
|
" button.attr('aria-disabled', 'false');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
"\n",
|
||
|
" var icon_img = $('<span/>');\n",
|
||
|
" icon_img.addClass('ui-button-icon-primary ui-icon');\n",
|
||
|
" icon_img.addClass(image);\n",
|
||
|
" icon_img.addClass('ui-corner-all');\n",
|
||
|
"\n",
|
||
|
" var tooltip_span = $('<span/>');\n",
|
||
|
" tooltip_span.addClass('ui-button-text');\n",
|
||
|
" tooltip_span.html(tooltip);\n",
|
||
|
"\n",
|
||
|
" button.append(icon_img);\n",
|
||
|
" button.append(tooltip_span);\n",
|
||
|
"\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fmt_picker_span = $('<span/>');\n",
|
||
|
"\n",
|
||
|
" var fmt_picker = $('<select/>');\n",
|
||
|
" fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
|
||
|
" fmt_picker_span.append(fmt_picker);\n",
|
||
|
" nav_element.append(fmt_picker_span);\n",
|
||
|
" this.format_dropdown = fmt_picker[0];\n",
|
||
|
"\n",
|
||
|
" for (var ind in mpl.extensions) {\n",
|
||
|
" var fmt = mpl.extensions[ind];\n",
|
||
|
" var option = $(\n",
|
||
|
" '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
|
||
|
" fmt_picker.append(option)\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add hover states to the ui-buttons\n",
|
||
|
" $( \".ui-button\" ).hover(\n",
|
||
|
" function() { $(this).addClass(\"ui-state-hover\");},\n",
|
||
|
" function() { $(this).removeClass(\"ui-state-hover\");}\n",
|
||
|
" );\n",
|
||
|
"\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
|
||
|
" // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
|
||
|
" // which will in turn request a refresh of the image.\n",
|
||
|
" this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_message = function(type, properties) {\n",
|
||
|
" properties['type'] = type;\n",
|
||
|
" properties['figure_id'] = this.id;\n",
|
||
|
" this.ws.send(JSON.stringify(properties));\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.send_draw_message = function() {\n",
|
||
|
" if (!this.waiting) {\n",
|
||
|
" this.waiting = true;\n",
|
||
|
" this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" var format_dropdown = fig.format_dropdown;\n",
|
||
|
" var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
|
||
|
" fig.ondownload(fig, format);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
|
||
|
" var size = msg['size'];\n",
|
||
|
" if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
|
||
|
" fig._resize_canvas(size[0], size[1]);\n",
|
||
|
" fig.send_message(\"refresh\", {});\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
|
||
|
" var x0 = msg['x0'];\n",
|
||
|
" var y0 = fig.canvas.height - msg['y0'];\n",
|
||
|
" var x1 = msg['x1'];\n",
|
||
|
" var y1 = fig.canvas.height - msg['y1'];\n",
|
||
|
" x0 = Math.floor(x0) + 0.5;\n",
|
||
|
" y0 = Math.floor(y0) + 0.5;\n",
|
||
|
" x1 = Math.floor(x1) + 0.5;\n",
|
||
|
" y1 = Math.floor(y1) + 0.5;\n",
|
||
|
" var min_x = Math.min(x0, x1);\n",
|
||
|
" var min_y = Math.min(y0, y1);\n",
|
||
|
" var width = Math.abs(x1 - x0);\n",
|
||
|
" var height = Math.abs(y1 - y0);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.clearRect(\n",
|
||
|
" 0, 0, fig.canvas.width, fig.canvas.height);\n",
|
||
|
"\n",
|
||
|
" fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
|
||
|
" // Updates the figure title.\n",
|
||
|
" fig.header.textContent = msg['label'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
|
||
|
" var cursor = msg['cursor'];\n",
|
||
|
" switch(cursor)\n",
|
||
|
" {\n",
|
||
|
" case 0:\n",
|
||
|
" cursor = 'pointer';\n",
|
||
|
" break;\n",
|
||
|
" case 1:\n",
|
||
|
" cursor = 'default';\n",
|
||
|
" break;\n",
|
||
|
" case 2:\n",
|
||
|
" cursor = 'crosshair';\n",
|
||
|
" break;\n",
|
||
|
" case 3:\n",
|
||
|
" cursor = 'move';\n",
|
||
|
" break;\n",
|
||
|
" }\n",
|
||
|
" fig.rubberband_canvas.style.cursor = cursor;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_message = function(fig, msg) {\n",
|
||
|
" fig.message.textContent = msg['message'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
|
||
|
" // Request the server to send over a new figure.\n",
|
||
|
" fig.send_draw_message();\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
|
||
|
" fig.image_mode = msg['mode'];\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Called whenever the canvas gets updated.\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// A function to construct a web socket function for onmessage handling.\n",
|
||
|
"// Called in the figure constructor.\n",
|
||
|
"mpl.figure.prototype._make_on_message_function = function(fig) {\n",
|
||
|
" return function socket_on_message(evt) {\n",
|
||
|
" if (evt.data instanceof Blob) {\n",
|
||
|
" /* FIXME: We get \"Resource interpreted as Image but\n",
|
||
|
" * transferred with MIME type text/plain:\" errors on\n",
|
||
|
" * Chrome. But how to set the MIME type? It doesn't seem\n",
|
||
|
" * to be part of the websocket stream */\n",
|
||
|
" evt.data.type = \"image/png\";\n",
|
||
|
"\n",
|
||
|
" /* Free the memory for the previous frames */\n",
|
||
|
" if (fig.imageObj.src) {\n",
|
||
|
" (window.URL || window.webkitURL).revokeObjectURL(\n",
|
||
|
" fig.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
|
||
|
" evt.data);\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
" else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
|
||
|
" fig.imageObj.src = evt.data;\n",
|
||
|
" fig.updated_canvas_event();\n",
|
||
|
" fig.waiting = false;\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var msg = JSON.parse(evt.data);\n",
|
||
|
" var msg_type = msg['type'];\n",
|
||
|
"\n",
|
||
|
" // Call the \"handle_{type}\" callback, which takes\n",
|
||
|
" // the figure and JSON message as its only arguments.\n",
|
||
|
" try {\n",
|
||
|
" var callback = fig[\"handle_\" + msg_type];\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" if (callback) {\n",
|
||
|
" try {\n",
|
||
|
" // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
|
||
|
" callback(fig, msg);\n",
|
||
|
" } catch (e) {\n",
|
||
|
" console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" };\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
|
||
|
"mpl.findpos = function(e) {\n",
|
||
|
" //this section is from http://www.quirksmode.org/js/events_properties.html\n",
|
||
|
" var targ;\n",
|
||
|
" if (!e)\n",
|
||
|
" e = window.event;\n",
|
||
|
" if (e.target)\n",
|
||
|
" targ = e.target;\n",
|
||
|
" else if (e.srcElement)\n",
|
||
|
" targ = e.srcElement;\n",
|
||
|
" if (targ.nodeType == 3) // defeat Safari bug\n",
|
||
|
" targ = targ.parentNode;\n",
|
||
|
"\n",
|
||
|
" // jQuery normalizes the pageX and pageY\n",
|
||
|
" // pageX,Y are the mouse positions relative to the document\n",
|
||
|
" // offset() returns the position of the element relative to the document\n",
|
||
|
" var x = e.pageX - $(targ).offset().left;\n",
|
||
|
" var y = e.pageY - $(targ).offset().top;\n",
|
||
|
"\n",
|
||
|
" return {\"x\": x, \"y\": y};\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"/*\n",
|
||
|
" * return a copy of an object with only non-object keys\n",
|
||
|
" * we need this to avoid circular references\n",
|
||
|
" * http://stackoverflow.com/a/24161582/3208463\n",
|
||
|
" */\n",
|
||
|
"function simpleKeys (original) {\n",
|
||
|
" return Object.keys(original).reduce(function (obj, key) {\n",
|
||
|
" if (typeof original[key] !== 'object')\n",
|
||
|
" obj[key] = original[key]\n",
|
||
|
" return obj;\n",
|
||
|
" }, {});\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.mouse_event = function(event, name) {\n",
|
||
|
" var canvas_pos = mpl.findpos(event)\n",
|
||
|
"\n",
|
||
|
" if (name === 'button_press')\n",
|
||
|
" {\n",
|
||
|
" this.canvas.focus();\n",
|
||
|
" this.canvas_div.focus();\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var x = canvas_pos.x;\n",
|
||
|
" var y = canvas_pos.y;\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {x: x, y: y, button: event.button,\n",
|
||
|
" step: event.step,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
"\n",
|
||
|
" /* This prevents the web browser from automatically changing to\n",
|
||
|
" * the text insertion cursor when the button is pressed. We want\n",
|
||
|
" * to control all of the cursor setting manually through the\n",
|
||
|
" * 'cursor' event from matplotlib */\n",
|
||
|
" event.preventDefault();\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" // Handle any extra behaviour associated with a key event\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.key_event = function(event, name) {\n",
|
||
|
"\n",
|
||
|
" // Prevent repeat events\n",
|
||
|
" if (name == 'key_press')\n",
|
||
|
" {\n",
|
||
|
" if (event.which === this._key)\n",
|
||
|
" return;\n",
|
||
|
" else\n",
|
||
|
" this._key = event.which;\n",
|
||
|
" }\n",
|
||
|
" if (name == 'key_release')\n",
|
||
|
" this._key = null;\n",
|
||
|
"\n",
|
||
|
" var value = '';\n",
|
||
|
" if (event.ctrlKey && event.which != 17)\n",
|
||
|
" value += \"ctrl+\";\n",
|
||
|
" if (event.altKey && event.which != 18)\n",
|
||
|
" value += \"alt+\";\n",
|
||
|
" if (event.shiftKey && event.which != 16)\n",
|
||
|
" value += \"shift+\";\n",
|
||
|
"\n",
|
||
|
" value += 'k';\n",
|
||
|
" value += event.which.toString();\n",
|
||
|
"\n",
|
||
|
" this._key_event_extra(event, name);\n",
|
||
|
"\n",
|
||
|
" this.send_message(name, {key: value,\n",
|
||
|
" guiEvent: simpleKeys(event)});\n",
|
||
|
" return false;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
|
||
|
" if (name == 'download') {\n",
|
||
|
" this.handle_save(this, null);\n",
|
||
|
" } else {\n",
|
||
|
" this.send_message(\"toolbar_button\", {name: name});\n",
|
||
|
" }\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
|
||
|
" this.message.textContent = tooltip;\n",
|
||
|
"};\n",
|
||
|
"mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
|
||
|
"\n",
|
||
|
"mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
|
||
|
"\n",
|
||
|
"mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
|
||
|
" // Create a \"websocket\"-like object which calls the given IPython comm\n",
|
||
|
" // object with the appropriate methods. Currently this is a non binary\n",
|
||
|
" // socket, so there is still some room for performance tuning.\n",
|
||
|
" var ws = {};\n",
|
||
|
"\n",
|
||
|
" ws.close = function() {\n",
|
||
|
" comm.close()\n",
|
||
|
" };\n",
|
||
|
" ws.send = function(m) {\n",
|
||
|
" //console.log('sending', m);\n",
|
||
|
" comm.send(m);\n",
|
||
|
" };\n",
|
||
|
" // Register the callback with on_msg.\n",
|
||
|
" comm.on_msg(function(msg) {\n",
|
||
|
" //console.log('receiving', msg['content']['data'], msg);\n",
|
||
|
" // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
|
||
|
" ws.onmessage(msg['content']['data'])\n",
|
||
|
" });\n",
|
||
|
" return ws;\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.mpl_figure_comm = function(comm, msg) {\n",
|
||
|
" // This is the function which gets called when the mpl process\n",
|
||
|
" // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
|
||
|
"\n",
|
||
|
" var id = msg.content.data.id;\n",
|
||
|
" // Get hold of the div created by the display call when the Comm\n",
|
||
|
" // socket was opened in Python.\n",
|
||
|
" var element = $(\"#\" + id);\n",
|
||
|
" var ws_proxy = comm_websocket_adapter(comm)\n",
|
||
|
"\n",
|
||
|
" function ondownload(figure, format) {\n",
|
||
|
" window.open(figure.imageObj.src);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var fig = new mpl.figure(id, ws_proxy,\n",
|
||
|
" ondownload,\n",
|
||
|
" element.get(0));\n",
|
||
|
"\n",
|
||
|
" // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
|
||
|
" // web socket which is closed, not our websocket->open comm proxy.\n",
|
||
|
" ws_proxy.onopen();\n",
|
||
|
"\n",
|
||
|
" fig.parent_element = element.get(0);\n",
|
||
|
" fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
|
||
|
" if (!fig.cell_info) {\n",
|
||
|
" console.error(\"Failed to find cell for figure\", id, fig);\n",
|
||
|
" return;\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" var output_index = fig.cell_info[2]\n",
|
||
|
" var cell = fig.cell_info[0];\n",
|
||
|
"\n",
|
||
|
"};\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_close = function(fig, msg) {\n",
|
||
|
" fig.root.unbind('remove')\n",
|
||
|
"\n",
|
||
|
" // Update the output cell to use the data from the current canvas.\n",
|
||
|
" fig.push_to_output();\n",
|
||
|
" var dataURL = fig.canvas.toDataURL();\n",
|
||
|
" // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
|
||
|
" // the notebook keyboard shortcuts fail.\n",
|
||
|
" IPython.keyboard_manager.enable()\n",
|
||
|
" $(fig.parent_element).html('<img src=\"' + dataURL + '\">');\n",
|
||
|
" fig.close_ws(fig, msg);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.close_ws = function(fig, msg){\n",
|
||
|
" fig.send_message('closing', msg);\n",
|
||
|
" // fig.ws.close()\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
|
||
|
" // Turn the data on the canvas into data in the output cell.\n",
|
||
|
" var dataURL = this.canvas.toDataURL();\n",
|
||
|
" this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\">';\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.updated_canvas_event = function() {\n",
|
||
|
" // Tell IPython that the notebook contents must change.\n",
|
||
|
" IPython.notebook.set_dirty(true);\n",
|
||
|
" this.send_message(\"ack\", {});\n",
|
||
|
" var fig = this;\n",
|
||
|
" // Wait a second, then push the new image to the DOM so\n",
|
||
|
" // that it is saved nicely (might be nice to debounce this).\n",
|
||
|
" setTimeout(function () { fig.push_to_output() }, 1000);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._init_toolbar = function() {\n",
|
||
|
" var fig = this;\n",
|
||
|
"\n",
|
||
|
" var nav_element = $('<div/>')\n",
|
||
|
" nav_element.attr('style', 'width: 100%');\n",
|
||
|
" this.root.append(nav_element);\n",
|
||
|
"\n",
|
||
|
" // Define a callback function for later on.\n",
|
||
|
" function toolbar_event(event) {\n",
|
||
|
" return fig.toolbar_button_onclick(event['data']);\n",
|
||
|
" }\n",
|
||
|
" function toolbar_mouse_event(event) {\n",
|
||
|
" return fig.toolbar_button_onmouseover(event['data']);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" for(var toolbar_ind in mpl.toolbar_items){\n",
|
||
|
" var name = mpl.toolbar_items[toolbar_ind][0];\n",
|
||
|
" var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
|
||
|
" var image = mpl.toolbar_items[toolbar_ind][2];\n",
|
||
|
" var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
|
||
|
"\n",
|
||
|
" if (!name) { continue; };\n",
|
||
|
"\n",
|
||
|
" var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
|
||
|
" button.click(method_name, toolbar_event);\n",
|
||
|
" button.mouseover(tooltip, toolbar_mouse_event);\n",
|
||
|
" nav_element.append(button);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
" // Add the status bar.\n",
|
||
|
" var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
|
||
|
" nav_element.append(status_bar);\n",
|
||
|
" this.message = status_bar[0];\n",
|
||
|
"\n",
|
||
|
" // Add the close button to the window.\n",
|
||
|
" var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
|
||
|
" var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
|
||
|
" button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
|
||
|
" button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
|
||
|
" buttongrp.append(button);\n",
|
||
|
" var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
|
||
|
" titlebar.prepend(buttongrp);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._root_extra_style = function(el){\n",
|
||
|
" var fig = this\n",
|
||
|
" el.on(\"remove\", function(){\n",
|
||
|
"\tfig.close_ws(fig, {});\n",
|
||
|
" });\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._canvas_extra_style = function(el){\n",
|
||
|
" // this is important to make the div 'focusable\n",
|
||
|
" el.attr('tabindex', 0)\n",
|
||
|
" // reach out to IPython and tell the keyboard manager to turn it's self\n",
|
||
|
" // off when our div gets focus\n",
|
||
|
"\n",
|
||
|
" // location in version 3\n",
|
||
|
" if (IPython.notebook.keyboard_manager) {\n",
|
||
|
" IPython.notebook.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
" else {\n",
|
||
|
" // location in version 2\n",
|
||
|
" IPython.keyboard_manager.register_events(el);\n",
|
||
|
" }\n",
|
||
|
"\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype._key_event_extra = function(event, name) {\n",
|
||
|
" var manager = IPython.notebook.keyboard_manager;\n",
|
||
|
" if (!manager)\n",
|
||
|
" manager = IPython.keyboard_manager;\n",
|
||
|
"\n",
|
||
|
" // Check for shift+enter\n",
|
||
|
" if (event.shiftKey && event.which == 13) {\n",
|
||
|
" this.canvas_div.blur();\n",
|
||
|
" event.shiftKey = false;\n",
|
||
|
" // Send a \"J\" for go to next cell\n",
|
||
|
" event.which = 74;\n",
|
||
|
" event.keyCode = 74;\n",
|
||
|
" manager.command_mode();\n",
|
||
|
" manager.handle_keydown(event);\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"mpl.figure.prototype.handle_save = function(fig, msg) {\n",
|
||
|
" fig.ondownload(fig, null);\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"mpl.find_output_cell = function(html_output) {\n",
|
||
|
" // Return the cell and output element which can be found *uniquely* in the notebook.\n",
|
||
|
" // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
|
||
|
" // IPython event is triggered only after the cells have been serialised, which for\n",
|
||
|
" // our purposes (turning an active figure into a static one), is too late.\n",
|
||
|
" var cells = IPython.notebook.get_cells();\n",
|
||
|
" var ncells = cells.length;\n",
|
||
|
" for (var i=0; i<ncells; i++) {\n",
|
||
|
" var cell = cells[i];\n",
|
||
|
" if (cell.cell_type === 'code'){\n",
|
||
|
" for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
|
||
|
" var data = cell.output_area.outputs[j];\n",
|
||
|
" if (data.data) {\n",
|
||
|
" // IPython >= 3 moved mimebundle to data attribute of output\n",
|
||
|
" data = data.data;\n",
|
||
|
" }\n",
|
||
|
" if (data['text/html'] == html_output) {\n",
|
||
|
" return [cell, data, j];\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
" }\n",
|
||
|
"}\n",
|
||
|
"\n",
|
||
|
"// Register the function which deals with the matplotlib target/channel.\n",
|
||
|
"// The kernel may be null if the page has been refreshed.\n",
|
||
|
"if (IPython.notebook.kernel != null) {\n",
|
||
|
" IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
|
||
|
"}\n"
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.Javascript object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"text/html": [
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoAAAAHgCAYAAAA10dzkAAAgAElEQVR4nO3deZhU5Z328Tsmyr4IBKOgjmLcxWjczauOCmoGGc1EXHHNRLTdOov7mmhcxphETRSTaOIyRp0YjahtUDFuRIhiXFBQCNgSXIFmp6Hrfv94GkXsahqeqn7qnPp+rquuQFHd/Tvh2P3l1DnPkQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJAPV0p6RVKDpBmS/ldS/1V8zFOSlkiaK2le8/+OKN+IAAAAKKUrJ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99s77RTWLPvhBPs556zC4XUk6FaEICIRQACQBsUCvbYsfaJJ4bo22kne+TIEINAeyMAEYsABIBWzJ5t33CDvd12dvfu9qmnhnX8gJQIQMQiAAFgJYWC/fzz9nHH2Z062bvvHu7QMX9+6smAgABELAIQAJrNmfPp0b4ePcLFHa+8knoq4PMIQMQiAAFUvfHj7ZNOCuf27bprONq3YEHqqYDiCEDEIgABVKX58+1f/9r++tftrl3tESM4tw/ZQQAiFgEIoK
|
||
|
],
|
||
|
"text/plain": [
|
||
|
"<IPython.core.display.HTML object>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Save a figure to a file\n",
|
||
|
"fig, ax = plt.subplots()\n",
|
||
|
"x = np.linspace(1, 10, 100)\n",
|
||
|
"y = np.log(x)\n",
|
||
|
"ax.plot(x, y)\n",
|
||
|
"fig.savefig(\"/tmp/out.png\")"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"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.4.2"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0
|
||
|
}
|