import json
import numpy as np
import matplotlib.pyplot as plt
import mpld3
from mpld3 import plugins, utils
class VoronoiHighlightLines(plugins.PluginBase):
"""A plugin to highlight lines on hover
see also http://bl.ocks.org/mbostock/8033015
"""
JAVASCRIPT = """
mpld3.register_plugin("voronoi_highlightlines", VoronoiHighlightLines);
VoronoiHighlightLines.prototype = Object.create(mpld3.Plugin.prototype);
VoronoiHighlightLines.prototype.constructor = VoronoiHighlightLines;
VoronoiHighlightLines.prototype.requiredProps = ["line_ids"];
VoronoiHighlightLines.prototype.defaultProps = {alpha_bg:0.3, alpha_fg:1.0}
function VoronoiHighlightLines(fig, props){
mpld3.Plugin.call(this, fig, props);
};
VoronoiHighlightLines.prototype.draw = function(){
var alpha_fg = this.props.alpha_fg;
var alpha_bg = this.props.alpha_bg;
// get the data for the voronoi mesh
data = new Array();
for(var i=0; i<this.props.line_ids.length; i++){
var line = mpld3.get_element(this.props.line_ids[i], this.fig);
console.log()
for (j=0; j<line.data.length; j++){
var obj = {}
obj.x = line.data[j][line.props.xindex]
obj.y = line.data[j][line.props.yindex]
obj.line = line
obj.line_id = this.props.line_ids[i]
obj.fig = this.fig
data.push(obj)
}
}
var ax = mpld3.get_element(this.props.line_ids[0], this.fig).ax
// we hide the transform from data coordinates to svg
// coordinates in the voronoi
var transform_x = function(d){return ax.x(d)+ax.position[0]};
var transform_y = function(d){return ax.y(d)+ax.position[1]};
var voronoi = d3.geom.voronoi()
.x(function(d) { return transform_x(d.x); })
.y(function(d) { return transform_y(d.y); })
.clipExtent([ax.position, [ax.position[0]+ax.width, ax.position[1]+ax.height]]);
var voronoiGroup = this.fig.canvas.append("svg:g")
.attr("class", "voronoi");
voronoiGroup.selectAll("path")
.data(voronoi(d3.nest()
.key(function(d) { return d.x + "," + d.y; })
.rollup(function(v) { return v[0]; })
.entries(data)
.map(function(d) { return d.values; })))
.enter().append("path")
.attr("d", function(d) {
var ret = "M" + d.join(" L") + "Z";
return ret; })
.datum(function(d) {return d.point; })
.on("mouseover", mouseover)
.on("mouseout", mouseout);
function mouseover(d) {
d3.select(d.line.path[0][0])
.style("stroke-opacity", alpha_fg);
}
function mouseout(d) {
d3.select(d.line.path[0][0])
.style("stroke-opacity", alpha_bg);
}
};
"""
def __init__(self, lines, css):
self.css_ = css or ""
self.lines = lines
self.dict_ = {"type": "voronoi_highlightlines",
"line_ids": [utils.get_id(line) for line in lines],
"alpha_bg": lines[0].get_alpha(),
"alpha_fg": 1.0}
# controls the coloring etc. of the voronoi mesh
css = """
.voronoi path
{
fill: none;
pointer-events: all;
stroke: red;
stroke-opacity: .1;
}
"""
np.random.seed(100)
N_paths = 50
N_steps = 100
x = np.linspace(0, 10, N_steps)
y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
y = y.cumsum(1)
fig, ax = plt.subplots()
lines = ax.plot(x, y.T, color='blue', lw=4, alpha=0.2)
plugins.clear(fig)
plugins.connect(fig, VoronoiHighlightLines(lines, css))
mpld3.display()
# controls the coloring etc. of the voronoi mesh
css = """
.voronoi path
{
fill: none;
pointer-events: all;
stroke: red;
stroke-opacity: .1;
}
"""
np.random.seed(100)
x = np.random.laplace(size=(2, 10))
y = np.random.laplace(size=(2, 10))
fig, ax = plt.subplots()
lines = ax.plot(x, y, 'k', linewidth=4, alpha=.5)
plugins.clear(fig)
plugins.connect(fig, VoronoiHighlightLines(lines, css))
mpld3.display()
fig, ax = plt.subplots()
rainbow = ax._get_lines.color_cycle
data = [ ]
for j in range(x.shape[1]):
ax.scatter(x[:, j],y[:, j], c=next(rainbow))
for j in range(x.shape[1]):
print "\n"
for entry in zip(x[:, j],y[:, j]):
print entry
(0.090811187405139118, -0.1468675047723893) (1.5262182962880018, 0.22025616614691021) (-0.58565914334428015, 2.1207606534486421) (-0.87130722231925228, 0.23270042066671262) (-0.16365465477992086, 1.0086767744100886) (-0.99247968557610522, -1.5592422087643882) (1.1697397158984613, -0.39716380906911775) (-1.5289932197434748, -0.26933555066322934) (-4.6630416606130405, -1.0474794208168889) (-0.82235653181868196, -2.6179520368504705) (-1.4141251020669192, -0.29348005822707729) (3.1523292294200234, 1.5178762353327664) (0.41778797792955563, -4.4761602128924887) (0.97648217706216389, 3.2660123501986558) (1.0547069220341905, -0.68348856177599038) (-1.0674566296583776, -2.1212308569366884) (-1.2967711504045869, 0.89483510131198474) (1.0008945478705795, 1.5191032237783906) (0.16273855226544243, -3.4897026651233123) (-0.60121087846120436, 0.16700308454559007)
[ 0.16273855 -0.60121088]