-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathplots.py
655 lines (484 loc) · 19.1 KB
/
plots.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
from lightning.types.base import Base
from lightning.types.decorators import viztype
from lightning.types.utils import array_to_lines, vecs_to_points, \
parse_links, add_property, mat_to_array, list_to_regions, parse_nodes, ndarray
@viztype
class Plot(Base):
_name = 'plot'
_options = dict(Base._options, **{
'type': {'default': None},
}
)
@staticmethod
def clean(data=None):
"""
Generic plotting function.
Provide arbitrary data and options objects as dictionaries,
and a plot type as a string. The data and options dictionary will be passed
directly to the plot, without any parsing or formatting,
so make sure it is of the appropriate for your visualization
(e.g. {"series": [1,2,3]} for a "line" visualization).
Most useful when providing data to custom visualizations, as opposed
to the included plot types (e.g. lightning.scatter, lightning.line, etc.)
which do automatic parsing and formatting.
Parameters
----------
data : dict
Dictionary with data to plot
type : str
Name of plot (e.g. 'line' or 'scatter')
"""
return data
@viztype
class Scatter(Base):
_name = 'scatter'
_options = dict(Base._options, **{
'tooltips': {'default': True},
'zoom': {'default': True},
'brush': {'default': True}
}
)
@staticmethod
def clean(x, y, labels=None, values=None, color=None, group=None, colormap=None,
size=None, alpha=None, xaxis=None, yaxis=None):
"""
Plot two-dimensional data as points.
.. image:: scatter.png
Parameters
----------
x, y : array-like, each (n,)
Input data
values : array-like, optional, singleton or (n,)
Values to set node colors via a linear scale
labels : array-like, optional, (n,)
Array of text labels to set tooltips
color : array-like, optional, singleton or (n,3)
Single rgb value or array to set colors
group : array-like, optional, singleton or (n,)
Single integer or array to set colors via groups
colormap : string
Specification of color map, only colorbrewer types supported
size : array-like, optional, singleton or (n,)
Single size or array to set point sizes
alpha : array-like, optional, singleton or (n,)
Single alpha value or array to set fill and stroke opacity
xaxis : str, optional, default = None
Label for x-axis
yaxis : str, optional, default = None
Label for y-axis
tooltips : boolean, optional, default=True
Whether to show tooltips
zoom : boolean, optional, default=True
Whether to allow zooming
brush : boolean, optional, default=True
Whether to support brushing
"""
points = vecs_to_points(x, y)
outdict = {'points': points}
outdict = add_property(outdict, color, 'color')
outdict = add_property(outdict, group, 'group')
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, values, 'values')
outdict = add_property(outdict, colormap, 'colormap')
outdict = add_property(outdict, size, 'size')
outdict = add_property(outdict, alpha, 'alpha')
outdict = add_property(outdict, xaxis, 'xaxis')
outdict = add_property(outdict, yaxis, 'yaxis')
return outdict
def selected(self):
"""
Selected points from scatter plot as indices
"""
user_data = self._get_user_data()['settings']
if 'selected' in user_data.keys():
return user_data['selected']
else:
return []
def points(self):
"""
Selected points from scatter plot as x,y coordinates
"""
user_data = self._get_user_data()['settings']
if 'x' in user_data.keys() and 'y' in user_data.keys():
return user_data['x'], user_data['y']
else:
return []
@viztype
class Matrix(Base):
_name = 'matrix'
_options = dict(Base._options, **{
'numbers': {'default': False}
}
)
@staticmethod
def clean(matrix, colormap=None, row_labels=None, column_labels=None):
"""
Visualize a dense matrix or table as a heat map.
.. image:: matrix.png
Parameters
----------
matrix : array-like (n,m)
Two-dimensional array of matrix data
row_labels : array-like (n,)
Array of rows to label columns
column_labels : array-like (m,)
Array of strings to label columns
colormap : string
Specification of color map, only colorbrewer types supported
numbers : boolean, optional, default=True
Whether to show numbers on cells
"""
matrix = mat_to_array(matrix)
outdict = {'matrix': matrix}
outdict = add_property(outdict, colormap, 'colormap')
outdict = add_property(outdict, row_labels, 'rowLabels')
outdict = add_property(outdict, column_labels, 'columnLabels')
return outdict
@viztype
class Adjacency(Base):
_name = 'adjacency'
_options = dict(Base._options, **{
'numbers': {'default': False},
'symmetric': {'default': True},
'sort': {'default': 'group'}
}
)
@staticmethod
def clean(conn, labels=None, group=None):
"""
Visualize a sparse adjacency matrix.
.. image:: adjacency.png
Parameters
----------
conn : array-like, (n,n) or (n,3) or (n,2)
Input connectivity data as either a matrix or a list of links.
Matrix can be binary or continuous valued. Links should contain
either 2 elements per link (source, target),
or 3 elements (source, target, value).
labels : array-like, (n,)
Text labels for each item (will label rows and columns)
group : array-like, optional, singleton or (n,)
Single integer or array to set colors via groups
sort : str, optional, default='group'
What to sort by, options are 'group' | 'degree'
numbers : boolean, optional, default=False
Whether to show numbers on cells
symmetric : boolean, optional, default=True
Whether to make links symmetrical
"""
links = parse_links(conn)
nodes = parse_nodes(conn)
outdict = {'links': links, 'nodes': nodes}
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, group, 'group')
return outdict
@viztype
class Line(Base):
_name = 'line'
_options = dict(Base._options, **{
'zoom': {'default': True}
}
)
@staticmethod
def clean(series, index=None, color=None, group=None, thickness=None, xaxis=None, yaxis=None):
"""
Plot one-dimensional series data as lines.
.. image:: line.png
Parameters
----------
series : array-like, (n,m)
Input data for line plot, typically n series each of length m.
Can also pass a list where each individual series is of a different length.
index : array-like, (m,)
Specify index for the x-axis of the line plot.
color : array-like, optional, singleton or (n,3)
Single rgb value or array to set line colors
group : array-like, optional, singleton or (n,)
Single integer or array to set line colors via group assignment
thickness : array-like, optional, singleton or (n,)
Single size or array to set line thickness
xaxis : str, optional, default = None
Label for x-axis
yaxis : str, optional, default = None
Label for y-axis
zoom : boolean, optional, default=True
Whether to allow zooming
"""
series = array_to_lines(series)
outdict = {'series': series}
outdict = add_property(outdict, color, 'color')
outdict = add_property(outdict, thickness, 'thickness')
outdict = add_property(outdict, group, 'group')
outdict = add_property(outdict, index, 'index')
outdict = add_property(outdict, xaxis, 'xaxis')
outdict = add_property(outdict, yaxis, 'yaxis')
return outdict
@viztype
class Force(Base):
_name = 'force'
_options = dict(Base._options, **{
'tooltips': {'default': True},
'zoom': {'default': True},
'brush': {'default': True}
}
)
@staticmethod
def clean(conn, values=None, labels=None, color=None, group=None, colormap=None, size=None):
"""
Create a force-directed network from connectivity.
.. image:: force.png
Parameters
----------
conn : array-like, (n,n) or (n,3) or (n,2)
Input connectivity data as either a matrix or a list of links.
Matrix can be binary or continuous valued. Links should contain
either 2 elements per link (source, target),
or 3 elements (source, target, value).
values : array-like, optional, singleton or (n,)
Values to set node colors via a linear scale
labels : array-like, optional, (n,)
Array of text labels to set tooltips
color : array-like, optional, singleton or (n,3)
Single rgb value or array to set node colors
group : array-like, optional, singleton or (n,)
Single integer or array to set node colors via group assignment
colormap : string
Specification of color map, only colorbrewer types supported
size : array-like, optional, singleton or (n,)
Single size or array to set node sizes
tooltips : boolean, optional, default=True
Whether to show tooltips
zoom : boolean, optional, default=True
Whether to allow zooming
brush : boolean, optional, default=True
Whether to support brushing
"""
links = parse_links(conn)
nodes = parse_nodes(conn)
outdict = {'links': links, 'nodes': nodes}
outdict = add_property(outdict, color, 'color')
outdict = add_property(outdict, group, 'group')
outdict = add_property(outdict, values, 'values')
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, colormap, 'colormap')
outdict = add_property(outdict, size, 'size')
return outdict
def selected(self):
"""
Selected points from force plot
"""
user_data = self._get_user_data()['settings']
if 'selected' in user_data.keys():
return user_data['selected']
else:
return []
@viztype
class Circle(Base):
_name = 'circle'
@staticmethod
def clean(conn, group=None, color=None, labels=None):
"""
Create a circular graph from connectivity data.
.. image:: circle.png
Parameters
----------
conn : array-like, (n,n) or (n,3) or (n,2)
Input connectivity data as either a matrix or a list of links.
Matrix can be binary or continuous valued. Links should contain
either 2 elements per link (source, target),
or 3 elements (source, target, value).
group : array-like, optional, (m,n) or (n,)
Hierarchical group assignments, where m is
the number of groups
color : array-like, optional, singleton or (k,3)
Single rgb value or array to set colors of top-level group,
where k is the number of unique elements in the top-level group
labels : array-like, optional, (n,)
Array of text labels to label nodes
"""
links = parse_links(conn)
nodes = parse_nodes(conn)
outdict = {'links': links, 'nodes': nodes}
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, color, 'color')
if group is not None:
if isinstance(group, ndarray):
group = group.tolist()
if isinstance(group, list):
if not isinstance(group[0], list):
if isinstance(group[0], ndarray):
group = [g.tolist() for g in group]
else:
group = [group]
else:
raise ValueError('group must be list or nested list')
outdict['group'] = group
return outdict
@viztype
class Graph(Base):
_name = 'graph'
_options = dict(Base._options, **{
'tooltips': {'default': True},
'zoom': {'default': True},
'brush': {'default': True}
}
)
@staticmethod
def clean(x, y, conn, values=None, labels=None,color=None, group=None, colormap=None, size=None):
"""
Create a node-link graph from spatial points and their connectivity.
.. image:: graph.png
Parameters
----------
x,y : array-like, each (n,)
Input data for nodes (x,y coordinates)
conn : array-like, (n,n) or (n,3) or (n,2)
Input connectivity data as either a matrix or a list of links.
Matrix can be binary or continuous valued. Links should contain
either 2 elements per link (source, target),
or 3 elements (source, target, value).
values : array-like, optional, singleton or (n,)
Values to set node colors via a linear scale
labels : array-like, optional, (n,)
Array of text labels to set tooltips
color : array-like, optional, singleton or (n,) or (n,3)
Single rgb value or array to set node colors
group : array-like, optional, singleton or (n,)
Single integer or array to set node colors via group assignment
colormap : string
Specification of color map, only colorbrewer types supported
size : array-like, optional, singleton or (n,)
Single size or array to set node sizes
tooltips : boolean, optional, default=True
Whether to show tooltips
zoom : boolean, optional, default=True
Whether to allow zooming
brush : boolean, optional, default=True
Whether to support brushing
"""
links = parse_links(conn)
nodes = vecs_to_points(x, y)
outdict = {'links': links, 'nodes': nodes}
outdict = add_property(outdict, color, 'color')
outdict = add_property(outdict, group, 'group')
outdict = add_property(outdict, values, 'values')
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, colormap, 'colormap')
outdict = add_property(outdict, size, 'size')
return outdict
@viztype
class GraphBundled(Base):
_name = 'graph-bundled'
_func = 'graphbundled'
_options = dict(Base._options, **{
'tooltips': {'default': True},
'zoom': {'default': True},
'brush': {'default': True}
}
)
@staticmethod
def clean(x, y, conn, labels=None, values=None, color=None, group=None, colormap=None, size=None):
"""
Create a bundled node-link graph from spatial points and their connectivity.
.. image:: graphbundled.png
Parameters
----------
x,y : array-like, each (n,)
Input data for nodes (x,y coordinates)
conn : array-like, (n,n) or (n,3) or (n,2)
Input connectivity data as either a matrix or a list of links.
Matrix can be binary or continuous valued. Links should contain
either 2 elements per link (source, target),
or 3 elements (source, target, value).
values : array-like, optional, singleton or (n,)
Values to set node colors via a linear scale
labels : array-like, optional, (n,)
Array of text labels to set tooltips
color : array-like, optional, singleton or (n,) or (n,3)
Single rgb value or array to set node colors
group : array-like, optional, singleton or (n,)
Single integer or array to set node colors via group assignment
colormap : string
Specification of color map, only colorbrewer types supported
size : array-like, optional, singleton or (n,)
Single size or array to set node sizes
tooltips : boolean, optional, default=True
Whether to show tooltips
zoom : boolean, optional, default=True
Whether to allow zooming
brush : boolean, optional, default=True
Whether to support brushing
"""
links = parse_links(conn)
points = vecs_to_points(x, y)
outdict = {'links': links, 'nodes': points}
outdict = add_property(outdict, color, 'color')
outdict = add_property(outdict, group, 'group')
outdict = add_property(outdict, values, 'values')
outdict = add_property(outdict, labels, 'labels')
outdict = add_property(outdict, colormap, 'colormap')
outdict = add_property(outdict, size, 'size')
return outdict
@viztype
class Map(Base):
_name = 'map'
@staticmethod
def clean(regions, values, colormap=None):
"""
Create a chloropleth map of the world or united states.
.. image:: map.png
Parameters
----------
regions : string or list
String identifiers for map regions, either length two strings (for states
in a US map) or length three strings (for countries in a world map)
weights : scalar or list
Values to use to color each region
colormap : string
Specification of color map, only colorbrewer types supported
"""
regions = list_to_regions(regions)
outdict = {'regions': regions}
outdict = add_property(outdict, values, 'values')
outdict = add_property(outdict, colormap, 'colormap')
return outdict
@viztype
class Histogram(Base):
_name = 'histogram'
_options = dict(Base._options, **{
'zoom': {'default': True}
}
)
@staticmethod
def clean(values, bins=None):
"""
Create a histogram.
.. image:: histogram.png
Parameters
----------
values : list
Values to plot a histogram of
bins : number, optional
Number of bins to used in the histogram. If unspecified
will default to sqrt(len(values))
"""
outdict = {'values': values}
outdict = add_property(outdict, bins, 'bins')
return outdict
@viztype
class VegaLite(Base):
_name = 'vega-lite'
_func = 'vega_lite'
@staticmethod
def clean(spec):
"""
Create a visualization from a vega-lite spec.
.. image:: vega-lite.png
Parameters
----------
values : spec
Vega-lite spec. Can be a dictionary, string, or Altiar object
"""
outdict = {}
outdict = add_property(outdict, spec, 'spec')
return outdict