Skip to content

Instantly share code, notes, and snippets.

@blink1073
Last active August 29, 2015 14:11
Show Gist options
  • Save blink1073/6e1c46bc77a7a201a4ae to your computer and use it in GitHub Desktop.
Save blink1073/6e1c46bc77a7a201a4ae to your computer and use it in GitHub Desktop.
MPLD3 Demos
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pylab as plt\n",
"import mpld3\n",
"mpld3.enable_notebook()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\"\"\"\n",
"Span Plugin\n",
"=================\n",
"This is a demo adapted from a `matplotlib gallery example\n",
"<http://matplotlib.org/examples/shapes_and_collections/path_patch_demo.html>`_\n",
"\n",
"This example adds a custom D3 plugin allowing the user to drag a\n",
"span and adjust the span shape.\n",
"\n",
"Use the toolbar buttons at the bottom-right of the plot to enable zooming\n",
"and panning, to reset the view, and to toggle the rectangle plugin.\n",
"\"\"\"\n",
"import matplotlib.pyplot as plt\n",
"import mpld3\n",
"from mpld3 import plugins\n",
"\n",
"\n",
"class MousePlugin(plugins.PluginBase):\n",
" JAVASCRIPT = r\"\"\"\n",
" mpld3.register_plugin(\"mouse\", MousePlugin);\n",
" MousePlugin.prototype = Object.create(mpld3.Plugin.prototype);\n",
" MousePlugin.prototype.constructor = MousePlugin;\n",
" MousePlugin.prototype.requiredProps = [];\n",
" MousePlugin.prototype.defaultProps = {fontsize: 12, fmt: \".3g\"};\n",
" function MousePlugin(fig, props){\n",
" mpld3.Plugin.call(this, fig, props);\n",
" };\n",
"\n",
" MousePlugin.prototype.draw = function(){\n",
" var fig = this.fig;\n",
" var fmt = d3.format(this.props.fmt);\n",
" var coords = fig.canvas.append(\"text\")\n",
" .attr(\"class\", \"mpld3-coordinates\")\n",
" .style(\"text-anchor\", \"end\")\n",
" .style(\"font-size\", this.props.fontsize)\n",
" .attr(\"x\", this.fig.width - 5)\n",
" .attr(\"y\", this.fig.height - 5);\n",
" var el = $('#' + fig.figid);\n",
" el.bind(\"howdy\", function () { console.log(\"howdy\") });\n",
" for (var i=0; i < this.fig.axes.length; i++) {\n",
" var update_coords = function() {\n",
" var ax = fig.axes[i];\n",
" return function() {\n",
" var pos = d3.mouse(this),\n",
" x = ax.x.invert(pos[0]),\n",
" y = ax.y.invert(pos[1]);\n",
" coords.text(\"(\" + fmt(x) + \", \" + fmt(y) + \")\");\n",
" };\n",
" }();\n",
" fig.axes[i].baseaxes\n",
" .on(\"mousemove\", update_coords)\n",
" .on(\"mouseout\", function() { coords.text(\"\"); el.trigger(\"howdy\"); })\n",
" .on(\"mousedown\", function () { console.log(\"mousedown\");});\n",
" }\n",
" };\n",
" \"\"\"\n",
" def __init__(self, fontsize=12, fmt=\".3g\"):\n",
" self.dict_ = {\"type\": \"mouse\",\n",
" \"fontsize\": fontsize,\n",
" \"fmt\": fmt}\n",
"\n",
"from skimage import data\n",
"fig, ax = plt.subplots()\n",
"plt.imshow(data.moon(), cmap='gray')\n",
"\n",
"from matplotlib.widgets import RectangleSelector\n",
"\n",
"r = RectangleSelector(ax, lambda: 1)\n",
"\n",
"def onselect(*pos):\n",
" print([int(float(p)) for p in pos])\n",
" print('hello, world!')\n",
"\n",
"plugins.connect(fig, MousePlugin())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment