!date
import numpy as np, matplotlib.pyplot as plt, mpld3
%matplotlib inline
mpld3.enable_notebook()
Tue Dec 2 06:07:27 PST 2014
from mpld3 import utils, plugins
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 = ["ids"];
function ClickInfo(fig, props){
mpld3.Plugin.call(this, fig, props);
};
ClickInfo.prototype.draw = function(){
this.props.ids.forEach(function(id, i) {
var obj = mpld3.get_element(id);
obj.elements().on("mousedown",
function(d){alert("clicked on bar[" + i + "]");});
});
}
"""
def __init__(self, bars):
self.dict_ = {"type": "clickinfo",
"ids": [utils.get_id(bar) for bar in bars]}
x = range(0,10)
y = np.random.rand(10)
fig, ax = plt.subplots()
bars = ax.bar(x, y)
plugins.connect(fig, ClickInfo(bars))