-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path03_09_iris_scatter_matrix.py
87 lines (70 loc) · 2.51 KB
/
03_09_iris_scatter_matrix.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
81
82
83
84
85
86
87
from bokeh.document import Document
from bokeh.embed import file_html
from bokeh.layouts import gridplot
from bokeh.models import (BasicTicker, Circle, ColumnDataSource, DataRange1d,
Grid, LinearAxis, PanTool, Plot, WheelZoomTool)
from bokeh.resources import INLINE
from bokeh.sampledata.iris import flowers
from bokeh.util.browser import view
colormap = {'setosa': 'red', 'versicolor': 'green', 'virginica': 'blue'}
flowers['color'] = flowers['species'].map(lambda x: colormap[x])
source = ColumnDataSource(
data=dict(
petal_length=flowers['petal_length'],
petal_width=flowers['petal_width'],
sepal_length=flowers['sepal_length'],
sepal_width=flowers['sepal_width'],
color=flowers['color']
)
)
xdr = DataRange1d(bounds=None)
ydr = DataRange1d(bounds=None)
def make_plot(xname, yname, xax=False, yax=False):
mbl = 40 if yax else 0
mbb = 40 if xax else 0
plot = Plot(
x_range=xdr, y_range=ydr, background_fill_color="#efe8e2",
border_fill_color='white', width=200 + mbl, height=200 + mbb,
min_border_left=2+mbl, min_border_right=2, min_border_top=2, min_border_bottom=2+mbb)
circle = Circle(x=xname, y=yname, fill_color="color", fill_alpha=0.2, radius=0.1, line_color="color")
r = plot.add_glyph(source, circle)
xdr.renderers.append(r)
ydr.renderers.append(r)
xticker = BasicTicker()
if xax:
xaxis = LinearAxis()
xaxis.axis_label = xname
plot.add_layout(xaxis, 'below')
xticker = xaxis.ticker
plot.add_layout(Grid(dimension=0, ticker=xticker))
yticker = BasicTicker()
if yax:
yaxis = LinearAxis()
yaxis.axis_label = yname
yaxis.major_label_orientation = 'vertical'
plot.add_layout(yaxis, 'left')
yticker = yaxis.ticker
plot.add_layout(Grid(dimension=1, ticker=yticker))
plot.add_tools(PanTool(), WheelZoomTool())
return plot
xattrs = ["petal_length", "petal_width", "sepal_width", "sepal_length"]
yattrs = list(reversed(xattrs))
plots = []
for y in yattrs:
row = []
for x in xattrs:
xax = (y == yattrs[-1])
yax = (x == xattrs[0])
plot = make_plot(x, y, xax, yax)
row.append(plot)
plots.append(row)
grid = gridplot(plots)
doc = Document()
doc.add_root(grid)
if __name__ == "__main__":
doc.validate()
filename = "iris_splom.html"
with open(filename, "w") as f:
f.write(file_html(doc, INLINE, "Iris Data SPLOM"))
print("Wrote %s" % filename)
view(filename)