-
Notifications
You must be signed in to change notification settings - Fork 353
/
test_custom_plugin.py
80 lines (64 loc) · 2.25 KB
/
test_custom_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Test the custom plugin demoed on
http://jakevdp.github.io/blog/2014/01/10/d3-plugins-truly-interactive/
"""
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from mpld3 import plugins, utils
import jinja2
import json
class LinkedView(plugins.PluginBase):
"""A simple plugin showing how multiple axes can be linked"""
JAVASCRIPT = """
var LinkedViewPlugin = function(fig, prop){
this.fig = fig;
this.prop = mpld3.process_props(this, prop, {},
["idpts", "idline", "data"]);
}
LinkedViewPlugin.prototype.draw = function(){
var pts = mpld3.get_element(this.prop.idpts);
var line = mpld3.get_element(this.prop.idline);
var data = this.prop.data;
function mouseover(d, i){
line.data = data[i];
line.elements().transition()
.attr("d", line.datafunc(line.data))
.style("stroke", this.style.fill);
}
pts.elements().on("mouseover", mouseover);
};
mpld3.register_plugin("linkedview", LinkedViewPlugin);
"""
def __init__(self, points, line, linedata):
if isinstance(points, matplotlib.lines.Line2D):
suffix = "pts"
else:
suffix = None
self.dict_ = {"type": "linkedview",
"idpts": utils.get_id(points, suffix),
"idline": utils.get_id(line),
"data": linedata}
def main():
fig, ax = plt.subplots(2)
# scatter periods and amplitudes
np.random.seed(0)
P = 0.2 + np.random.random(size=20)
A = np.random.random(size=20)
x = np.linspace(0, 10, 100)
data = np.array([[x, Ai * np.sin(x / Pi)]
for (Ai, Pi) in zip(A, P)])
points = ax[1].scatter(P, A, c=P + A,
s=200, alpha=0.5)
ax[1].set_xlabel('Period')
ax[1].set_ylabel('Amplitude')
# create the line object
lines = ax[0].plot(x, 0 * x, '-w', lw=3, alpha=0.5)
ax[0].set_ylim(-1, 1)
ax[0].set_title("Hover over points to see lines")
# transpose line data and add plugin
linedata = data.transpose(0, 2, 1).tolist()
plugins.connect(fig, LinkedView(points, lines[0], linedata))
return fig
if __name__ == '__main__':
main()
plt.show()