%matplotlib inline import numpy as np import matplotlib.pyplot as plt import mpld3 mpld3.enable_notebook() np.random.seed(0) plt.plot(np.random.rand(10)); from mpld3 import plugins fig, ax = plt.subplots() ax.plot(np.random.random(10)) plugins.clear(fig) # clear all plugins from the figure from mpld3 import plugins fig, ax = plt.subplots() ax.plot(np.random.random(10)) plugins.clear(fig) # clear all plugins from the figure plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(), plugins.Zoom()) fig, ax = plt.subplots() points = ax.scatter(np.random.rand(40), np.random.rand(40), s=300, alpha=0.3) labels = ["Point {0}".format(i) for i in range(40)] tooltip = plugins.PointLabelTooltip(points, labels) plugins.connect(fig, tooltip) class HelloWorld(plugins.PluginBase): # inherit from PluginBase """Hello World plugin""" JAVASCRIPT = """ mpld3.register_plugin("helloworld", HelloWorld); HelloWorld.prototype = Object.create(mpld3.Plugin.prototype); HelloWorld.prototype.constructor = HelloWorld; function HelloWorld(fig, props){ mpld3.Plugin.call(this, fig, props); }; HelloWorld.prototype.draw = function(){ this.fig.canvas.append("text") .text("hello world") .style("font-size", 72) .style("opacity", 0.3) .style("text-anchor", "middle") .attr("x", this.fig.width / 2) .attr("y", this.fig.height / 2) } """ def __init__(self): self.dict_ = {"type": "helloworld"} fig, ax = plt.subplots() plugins.connect(fig, HelloWorld()) print(mpld3.fig_to_html(fig, template_type="simple")) from mpld3 import utils class ClickInfo(plugins.PluginBase): """Plugin for getting info on click""" JAVASCRIPT = """ mpld3.register_plugin("clickinfo", ClickInfo); ClickInfo.prototype = Object.create(mpld3.Plugin.prototype); ClickInfo.prototype.constructor = ClickInfo; ClickInfo.prototype.requiredProps = ["id"]; function ClickInfo(fig, props){ mpld3.Plugin.call(this, fig, props); }; ClickInfo.prototype.draw = function(){ var obj = mpld3.get_element(this.props.id); obj.elements().on("mousedown", function(d, i){alert("clicked on points[" + i + "]");}); } """ def __init__(self, points): self.dict_ = {"type": "clickinfo", "id": utils.get_id(points)} fig, ax = plt.subplots() points = ax.scatter(np.random.rand(50), np.random.rand(50), s=500, alpha=0.3) plugins.connect(fig, ClickInfo(points)) utils.get_id(points)