-
Notifications
You must be signed in to change notification settings - Fork 41
/
mark.py
942 lines (803 loc) · 33.6 KB
/
mark.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# Copyright 2014, Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain
# rights in this software.
"""Provides data objects (marks) that are displayed on a :class:`canvas <toyplot.canvas.Canvas>`."""
from __future__ import absolute_import, division
import itertools
import custom_inherit
import numpy
import six
import toyplot.color
import toyplot.marker
import toyplot.require
##########################################################################
# Basic Toyplot marks
@six.add_metaclass(custom_inherit.DocInheritMeta(style="numpy_napoleon"))
class Mark(object):
"""Abstract interface for Toyplot marks.
Marks are data objects that are added to a coordinate system for display on
a :class:`canvas <toyplot.canvas.Canvas>`. Marks carry no explicit visual
representation of their own - it is up to the coordinate system and
:ref:`rendering backend<backends>` to determine how to render the data.
For example, a :class:`scatterplot <toyplot.mark.Scatterplot>` mark is
rendered using points by a :class:`cartesian
<toyplot.coordinates.Cartesian>` coordinate system, but could be rendered
using lines by a hypothetical parallel coordinate system.
"""
def __init__(self, annotation=False):
self._annotation = False
self.annotation = annotation
@property
def annotation(self):
return self._annotation
@annotation.setter
def annotation(self, value):
self._annotation = True if value else False
def _finalize(self):
return self
def domain(self, axis): # pylint: disable=no-self-use
"""Return minimum and maximum domain values for the mark along the given axis.
Parameters
----------
axis: string, required
Name of an axis along which to return domain values.
Returns
-------
minimum: minimum domain value along the given axis, or `None`.
maximum: maximum domain value along the given axis, or `None`.
"""
return (None, None)
def extents(self, axes): # pylint: disable=no-self-use
"""Return range extents for the mark using the given axes.
Parameters
----------
axes: sequence of strings, required
Specifies the order in which domain coordinates must be returned.
Returns
-------
coordinates: tuple containing arrays of coordinates, in the order specified by the `axes` parameter.
extents: (left, right, top, bottom) tuple of arrays containing the extents of each datum in range-space, relative to the domain coordinates.
"""
empty = numpy.array([])
return tuple([empty] * len(axes)), tuple([empty] * 4)
@property
def markers(self): # pylint: disable=no-self-use
"""Return an ordered set of markers used by this mark, if any.
Returns
-------
markers: list of :class:`toyplot.marker.Marker` objects.
"""
return []
class AxisLines(Mark):
"""Render multiple lines parallel to an axis.
Do not create AxisLines instances directly. Use factory methods such as
:meth:`toyplot.coordinates.Cartesian.hlines` and :meth:`toyplot.coordinates.Cartesian.vlines` instead.
"""
def __init__(
self,
coordinate_axes,
table,
coordinates,
stroke,
opacity,
title,
style,
annotation,
):
Mark.__init__(self, annotation)
# 1 axis identifier
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=1)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._coordinates = toyplot.require.table_keys(table, coordinates, length=1)
# 1 stroke color column
self._stroke = toyplot.require.table_keys(table, stroke, length=1)
# 1 opacity column
self._opacity = toyplot.require.table_keys(table, opacity, length=1)
# 1 title column
self._title = toyplot.require.table_keys(table, title, length=1)
# Line style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.line)
def domain(self, axis):
if axis == self._coordinate_axes:
return toyplot.data.minimax([self._table[self._coordinates[0]]])
return (None, None)
class BarBoundaries(Mark):
"""Render multiple stacked bars defined by bar boundaries.
Do not create BarBoundaries instances directly. Use factory methods such as
:func:`toyplot.bars` or :meth:`toyplot.coordinates.Cartesian.bars` instead.
"""
def __init__(
self,
coordinate_axes,
table,
left,
right,
boundaries,
fill,
opacity,
title,
style,
filename,
):
Mark.__init__(self)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._left = toyplot.require.table_keys(table, left, length=1)
# 1 coordinate column
self._right = toyplot.require.table_keys(table, right, length=1)
# N bar boundary columns
self._boundaries = toyplot.require.table_keys(table, boundaries, min_length=2)
# N-1 fill color columns
self._fill = toyplot.require.table_keys(table, fill, length=len(boundaries) - 1)
# N-1 opacity columns
self._opacity = toyplot.require.table_keys(table, opacity, length=len(boundaries) - 1)
# N-1 title columns
self._title = toyplot.require.table_keys(table, title, length=len(boundaries) - 1)
# Bar style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.fill)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._left[0]], self._table[self._right[0]]])
if axis == self._coordinate_axes[1]:
return toyplot.data.minimax([self._table[key] for key in self._boundaries])
@property
def markers(self):
result = []
for fill, opacity in zip(
[self._table[key] for key in self._fill],
[self._table[key] for key in self._opacity],
):
result.append(toyplot.marker.create(shape="s", mstyle=toyplot.style.combine(
{
"fill": toyplot.color.to_css(fill[0]),
"fill-opacity": opacity[0],
},
self._style,
),
))
return result
class BarMagnitudes(Mark):
"""Render multiple stacked bars defined by bar magnitudes.
Do not create BarMagnitudes instances directly. Use factory methods such as
:func:`toyplot.bars` or :meth:`toyplot.coordinates.Cartesian.bars` instead.
"""
def __init__(
self,
coordinate_axes,
table,
left,
right,
baseline,
magnitudes,
fill,
opacity,
title,
style,
filename,
):
Mark.__init__(self)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._left = toyplot.require.table_keys(table, left, length=1)
# 1 coordinate column
self._right = toyplot.require.table_keys(table, right, length=1)
# 1 baseline column
self._baseline = toyplot.require.table_keys(table, baseline, length=1)
# N bar magnitude columns
self._magnitudes = toyplot.require.table_keys(table, magnitudes, min_length=1)
# N fill color columns
self._fill = toyplot.require.table_keys(table, fill, length=len(magnitudes))
# N opacity columns
self._opacity = toyplot.require.table_keys(table, opacity, length=len(magnitudes))
# N title columns
self._title = toyplot.require.table_keys(table, title, length=len(magnitudes))
# Bar style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.fill)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._left[0]], self._table[self._right[0]]])
if axis == self._coordinate_axes[1]:
boundaries = numpy.column_stack([self._table[key] for key in self._magnitudes])
boundaries = numpy.column_stack((self._table[self._baseline[0]], boundaries))
boundaries = numpy.cumsum(boundaries, axis=1)
return toyplot.data.minimax([boundaries])
@property
def markers(self):
result = []
for fill, opacity in zip(
[self._table[key] for key in self._fill],
[self._table[key] for key in self._opacity],
):
result.append(toyplot.marker.create(shape="s", mstyle=toyplot.style.combine(
{
"fill": toyplot.color.to_css(fill[0]),
"fill-opacity": opacity[0],
},
self._style,
),
))
return result
class FillBoundaries(Mark):
"""Render multiple stacked fill regions defined by boundaries.
Do not create FillBoundaries instances directly. Use factory methods such
as :func:`toyplot.fill` or :meth:`toyplot.coordinates.Cartesian.fill` instead.
"""
def __init__(
self,
coordinate_axes,
table,
position,
boundaries,
fill,
opacity,
title,
style,
annotation,
filename,
):
Mark.__init__(self, annotation)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._position = toyplot.require.table_keys(table, position, length=1)
# N fill boundary columns
self._boundaries = toyplot.require.table_keys(table, boundaries, min_length=2)
# N-1 fill colors
self._fill = toyplot.require.vector(fill, length=len(boundaries) - 1)
# N-1 opacities
self._opacity = toyplot.require.scalar_vector(opacity, length=len(boundaries) - 1)
# N-1 titles
#self._title = toyplot.require.object_vector(title, length=len(boundaries) - 1)
self._title = title
# Fill style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.fill)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._position[0]]])
if axis == self._coordinate_axes[1]:
return toyplot.data.minimax([self._table[key] for key in self._boundaries])
@property
def markers(self):
result = []
for fill, opacity in zip(
self._fill,
self._opacity,
):
result.append(toyplot.marker.create(shape="s", mstyle=toyplot.style.combine(
{
"fill": toyplot.color.to_css(fill),
"fill-opacity": opacity,
},
self._style,
),
))
return result
class FillMagnitudes(Mark):
"""Render multiple stacked fill regions defined by magnitudes.
Do not create FillMagnitudes instances directly. Use factory methods such
as :func:`toyplot.fill` or :meth:`toyplot.coordinates.Cartesian.fill` instead.
"""
def __init__(
self,
coordinate_axes,
table,
position,
baseline,
magnitudes,
fill,
opacity,
title,
style,
annotation,
filename,
):
Mark.__init__(self, annotation)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._position = toyplot.require.table_keys(table, position, length=1)
# 1 baseline column
self._baseline = toyplot.require.table_keys(table, baseline, length=1)
# N fill magnitude columns
self._magnitudes = toyplot.require.table_keys(table, magnitudes)
# N fill colors
self._fill = toyplot.require.vector(fill, length=len(magnitudes))
# N opacities
self._opacity = toyplot.require.scalar_vector(opacity, length=len(magnitudes))
# N titles
#self._title = toyplot.require.object_vector(title, length=len(magnitudes))
self._title = title
# Fill style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.fill)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._position[0]]])
if axis == self._coordinate_axes[1]:
boundaries = numpy.column_stack([self._table[key] for key in self._magnitudes])
boundaries = numpy.column_stack((self._table[self._baseline[0]], boundaries))
boundaries = numpy.cumsum(boundaries, axis=1)
return toyplot.data.minimax([boundaries])
@property
def markers(self):
result = []
for fill, opacity in zip(
self._fill,
self._opacity,
):
result.append(toyplot.marker.create(shape="s", mstyle=toyplot.style.combine(
{
"fill": toyplot.color.to_css(fill),
"fill-opacity": opacity,
},
self._style,
),
))
return result
class Graph(Mark): # pragma: no cover
"""Plot a graph (collection of vertices and edges).
Do not create Graph instances directly. Use factory methods such as
:meth:`toyplot.coordinates.Cartesian.graph` instead.
"""
def __init__(
self,
coordinate_axes,
ecolor,
ecoordinates,
efilename,
eopacity,
eshape,
esource,
estyle,
etable,
etarget,
ewidth,
hmarker,
mmarker,
mposition,
tmarker,
vcolor,
vcoordinates,
vfilename,
vid,
vlabel,
vlshow,
vlstyle,
vmarker,
vopacity,
vsize,
vstyle,
vtable,
vtitle,
):
Mark.__init__(self)
# D axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, min_length=1)
self._vtable = toyplot.require.instance(vtable, toyplot.data.Table)
# 1 vertex id column
self._vid = toyplot.require.table_keys(vtable, vid, length=1)
# 1 vertex label column
self._vlabel = toyplot.require.table_keys(vtable, vlabel, length=1)
# D coordinate columns
self._vcoordinates = toyplot.require.table_keys(vtable, vcoordinates, length=len(self._coordinate_axes))
# 1 vertex marker column
self._vmarker = toyplot.require.table_keys(vtable, vmarker, length=1)
# 1 vertex marker size column
self._vsize = toyplot.require.table_keys(vtable, vsize, length=1)
# 1 vertex marker color column
self._vcolor = toyplot.require.table_keys(vtable, vcolor, length=1)
# 1 marker opacity column
self._vopacity = toyplot.require.table_keys(vtable, vopacity, length=1)
# 1 vertex titles column
self._vtitle = toyplot.require.table_keys(vtable, vtitle, length=1)
# Vertex marker style
self._vstyle = toyplot.style.require(vstyle, allowed=toyplot.style.allowed.marker)
# Vertex marker label style
self._vlstyle = toyplot.style.require(vlstyle, allowed=toyplot.style.allowed.text)
# Draw vertex labels
self._vlshow = vlshow
# Export filename
self._vfilename = toyplot.require.filename(vfilename)
self._etable = toyplot.require.instance(etable, toyplot.data.Table)
# 1 edge source column
self._esource = toyplot.require.table_keys(etable, esource, length=1)
# 1 edge target column
self._etarget = toyplot.require.table_keys(etable, etarget, length=1)
# 1 edge shape column
self._eshape = toyplot.require.table_keys(etable, eshape, length=1)
ecoordinate_count = 0
for shape in self._etable[self._eshape[0]]:
for segment in shape:
if segment == "M":
ecoordinate_count += 1
elif segment == "L":
ecoordinate_count += 1
elif segment == "Q":
ecoordinate_count += 2
elif segment == "C":
ecoordinate_count += 3
else:
raise ValueError("Unknown edge shape segment: %s" % segment)
# C x D edge coordinate columns
self._ecoordinates = toyplot.require.scalar_matrix(ecoordinates, rows=ecoordinate_count, columns=len(self._coordinate_axes))
# 1 edge color column
self._ecolor = toyplot.require.table_keys(etable, ecolor, length=1)
# 1 edge width column
self._ewidth = toyplot.require.table_keys(etable, ewidth, length=1)
# 1 edge opacity column
self._eopacity = toyplot.require.table_keys(etable, eopacity, length=1)
# Edge style
self._estyle = toyplot.style.require(estyle, allowed=toyplot.style.allowed.line)
# Export filename
self._efilename = toyplot.require.filename(efilename)
# 1 head marker column
self._hmarker = toyplot.require.table_keys(etable, hmarker, length=1)
# 1 middle marker column
self._mmarker = toyplot.require.table_keys(etable, mmarker, length=1)
# 1 middle marker position column
self._mposition = toyplot.require.table_keys(etable, mposition, length=1)
# 1 tail marker column
self._tmarker = toyplot.require.table_keys(etable, tmarker, length=1)
def domain(self, axis):
index = numpy.flatnonzero(self._coordinate_axes == axis)[0]
return toyplot.data.minimax([self._vtable[self._vcoordinates[index]], self._ecoordinates[:, index]])
@property
def vcount(self):
"""Return the number of vertices in the graph."""
return len(self._vtable)
@property
def vids(self):
"""Returns the graph vertex identifiers."""
return self._vtable[self._vid[0]]
@property
def vcoordinates(self):
"""Return the graph vertex coordinates."""
return numpy.column_stack([self._vtable[column] for column in self._vcoordinates])
@property
def ecount(self):
"""Return the number of edges in the graph."""
return len(self._etable)
@property
def esources(self):
return self._etable[self._esource[0]]
@property
def etargets(self):
return self._etable[self._etarget[0]]
@property
def eshapes(self):
return self._etable[self._eshape[0]]
@property
def ecoordinates(self):
return self._ecoordinates
@property
def edges(self):
"""Return the graph edges as a :math:`E \\times 2` matrix of source, target indices."""
return numpy.column_stack((self._etable[self._esource[0]], self._etable[self._etarget[0]]))
class Image(Mark):
"""Plot a bitmap image.
Do not create Image instances directly. Use factory methods such as
:func:`toyplot.image` and :func:`toyplot.canvas.Canvas.image` instead.
"""
def __init__(
self,
xmin_range,
xmax_range,
ymin_range,
ymax_range,
data,
):
Mark.__init__(self)
self._xmin_range = xmin_range
self._xmax_range = xmax_range
self._ymin_range = ymin_range
self._ymax_range = ymax_range
data = numpy.atleast_3d(data)
if data.ndim != 3:
raise ValueError("Image must be a 1D, 2D or 3D array.")
if data.shape[2] < 1 or data.shape[2] > 4:
raise ValueError("Image must contain 1, 2, 3, or 4 channels.")
if issubclass(data.dtype.type, (numpy.object_, numpy.complexfloating, numpy.flexible)) and data.dtype != toyplot.color.dtype:
raise ValueError("Unsupported image dtype: %s" % data.dtype)
self._data = data
def to_png(self):
import io
import png
stream = io.BytesIO()
data = self._data
toyplot.log.debug("Image data: %s %s", data.shape, data.dtype)
if data.dtype == toyplot.color.dtype:
data = numpy.dstack((data["r"], data["g"], data["b"], data["a"]))
if issubclass(data.dtype.type, numpy.bool_):
bitdepth = 1
elif issubclass(data.dtype.type, numpy.floating):
data = (data * 255.0).astype("uint8")
bitdepth = 8
else:
bitdepth = 8
width = data.shape[1]
height = data.shape[0]
greyscale = data.shape[2] < 3
alpha = data.shape[2] == 2 or data.shape[2] == 4
writer = png.Writer(width=width, height=height, greyscale=greyscale, alpha=alpha, bitdepth=bitdepth)
writer.write(stream, numpy.reshape(data, (-1, data.shape[1] * data.shape[2])))
return stream.getvalue()
def to_data_url(self):
import base64
return "data:image/png;base64," + base64.standard_b64encode(self.to_png()).decode("ascii")
class Plot(Mark):
"""Plot multiple bivariate data series using lines and/or markers.
Do not create Plot instances directly. Use factory methods such as
:func:`toyplot.plot`, :func:`toyplot.scatterplot`,
:meth:`toyplot.coordinates.Cartesian.plot` and
:meth:`toyplot.coordinates.Cartesian.scatterplot` instead.
"""
def __init__(
self,
coordinate_axes,
table,
coordinates,
series,
stroke,
stroke_width,
stroke_opacity,
stroke_title,
marker,
msize,
mfill,
mstroke,
mopacity,
mtitle,
style,
mstyle,
mlstyle,
filename,
):
Mark.__init__(self)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._coordinates = toyplot.require.table_keys(table, coordinates, length=1)
# N coordinate columns
self._series = toyplot.require.table_keys(table, series, min_length=1)
# N stroke colors
self._stroke = toyplot.require.vector(stroke, length=len(series))
# N stroke widths
self._stroke_width = toyplot.require.scalar_vector(stroke_width, length=len(series))
# N stroke opacities
self._stroke_opacity = toyplot.require.scalar_vector(stroke_opacity, length=len(series))
# N stroke titles
self._stroke_title = stroke_title
# N marker columns
self._marker = toyplot.require.table_keys(table, marker, length=len(series))
# N marker size columns
self._msize = toyplot.require.table_keys(table, msize, length=len(series))
# N marker fill color columns
self._mfill = toyplot.require.table_keys(table, mfill, length=len(series))
# N marker stroke color columns
self._mstroke = toyplot.require.table_keys(table, mstroke, length=len(series))
# N marker opacity columns
self._mopacity = toyplot.require.table_keys(table, mopacity, length=len(series))
# N marker title columns
self._mtitle = toyplot.require.table_keys(table, mtitle, length=len(series))
# Line style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.line)
# Marker style
self._mstyle = toyplot.style.require(mstyle, allowed=toyplot.style.allowed.marker)
# Marker label style
self._mlstyle = toyplot.style.require(mlstyle, allowed=toyplot.style.allowed.text)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._coordinates[0]]])
if axis == self._coordinate_axes[1]:
return toyplot.data.minimax([self._table[key] for key in self._series])
@property
def markers(self):
result = []
for stroke, stroke_width, stroke_opacity in zip(self._stroke.T, self._stroke_width.T, self._stroke_opacity.T):
result.append(toyplot.marker.create(shape="/", mstyle=toyplot.style.combine(
{
"fill": toyplot.color.to_css(stroke),
"stroke": toyplot.color.to_css(stroke),
"stroke-width": stroke_width,
"stroke-opacity": stroke_opacity,
},
self._style,
),
))
return result
class Rect(Mark):
"""Plot axis-aligned rectangles.
Do not create Rect instances directly. Use factory methods such as
:meth:`toyplot.coordinates.Cartesian.rects` instead.
"""
def __init__(
self,
coordinate_axes,
table,
left,
right,
top,
bottom,
fill,
opacity,
title,
style,
filename,
):
Mark.__init__(self)
# 2 axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, length=2)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# 1 coordinate column
self._left = toyplot.require.table_keys(table, left, length=1)
# 1 coordinate column
self._right = toyplot.require.table_keys(table, right, length=1)
# 1 coordinate column
self._top = toyplot.require.table_keys(table, top, length=1)
# 1 coordinate column
self._bottom = toyplot.require.table_keys(table, bottom, length=1)
# 1 fill color column
self._fill = toyplot.require.table_keys(table, fill, length=1)
# 1 opacity column
self._opacity = toyplot.require.table_keys(table, opacity, length=1)
# 1 title column
self._title = toyplot.require.table_keys(table, title, length=1)
# Rectangle style
self._style = toyplot.style.require(style, allowed=toyplot.style.allowed.fill)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
if axis == self._coordinate_axes[0]:
return toyplot.data.minimax([self._table[self._left[0]], self._table[self._right[0]]])
if axis == self._coordinate_axes[1]:
return toyplot.data.minimax([self._table[self._top[0]], self._table[self._bottom[0]]])
class Scatterplot(Mark):
"""Plot multivariate data series using markers.
Do not create Scatterplot instances directly. Use factory methods such as
:func:`toyplot.scatterplot` and :meth:`toyplot.coordinates.Cartesian.scatterplot`
instead.
"""
def __init__(
self,
coordinate_axes,
coordinates,
filename,
marker,
mfill,
mhyperlink,
mlstyle,
mopacity,
msize,
mstroke,
mstyle,
mtitle,
table,
):
Mark.__init__(self)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# We require at last one coordinate dimension (number of dimensions = D)
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, min_length=1)
D = len(self._coordinate_axes)
# We require D coordinate columns for each series (number of series = N)
self._coordinates = toyplot.require.table_keys(table, coordinates, modulus=D)
N = len(self._coordinates) / D
# We require 1 export filename
self._filename = toyplot.require.filename(filename)
# We require 1 marker style
self._mstyle = toyplot.style.require(mstyle, allowed=toyplot.style.allowed.marker)
# We require 1 marker label style
self._mlstyle = toyplot.style.require(mlstyle, allowed=toyplot.style.allowed.text)
# We require N marker columns
self._marker = toyplot.require.table_keys(table, marker, length=N)
# We require N marker fill color columns
self._mfill = toyplot.require.table_keys(table, mfill, length=N)
# We require N marker hyperlink columns
self._mhyperlink = toyplot.require.table_keys(table, mhyperlink, length=N)
# We require N marker opacity columns
self._mopacity = toyplot.require.table_keys(table, mopacity, length=N)
# We require N marker size columns
self._msize = toyplot.require.table_keys(table, msize, length=N)
# We require N marker stroke color columns
self._mstroke = toyplot.require.table_keys(table, mstroke, length=N)
# We require N marker title columns
self._mtitle = toyplot.require.table_keys(table, mtitle, length=N)
def domain(self, axis):
columns = [coordinate_column for coordinate_axis, coordinate_column in zip(itertools.cycle(self._coordinate_axes), self._coordinates) if coordinate_axis == axis]
return toyplot.data.minimax([self._table[column] for column in columns])
@property
def markers(self):
result = []
for marker, mfill, mstroke, mopacity in zip(
[self._table[key] for key in self._marker],
[self._table[key] for key in self._mfill],
[self._table[key] for key in self._mstroke],
[self._table[key] for key in self._mopacity],
):
for dmarker, dfill, dstroke, dopacity in zip(marker, mfill, mstroke, mopacity):
mstyle = toyplot.style.combine(
{
"fill": toyplot.color.to_css(dfill),
"stroke": toyplot.color.to_css(dstroke),
"opacity": dopacity,
},
self._mstyle
)
dmarker = toyplot.marker.create(mstyle=mstyle, lstyle=self._mlstyle) + toyplot.marker.convert(dmarker)
result.append(dmarker)
break
return result
class Text(Mark):
"""Render text.
Do not create Text instances directly. Use factory methods such as
:meth:`toyplot.canvas.Canvas.text` or :meth:`toyplot.coordinates.Cartesian.text` instead.
"""
def __init__(
self,
coordinate_axes,
table,
coordinates,
text,
angle,
fill,
opacity,
title,
style,
annotation,
filename,
):
Mark.__init__(self, annotation)
# D axis identifiers
self._coordinate_axes = toyplot.require.string_vector(coordinate_axes, min_length=1)
self._table = toyplot.require.instance(table, toyplot.data.Table)
# D coordinate columns
self._coordinates = toyplot.require.table_keys(table, coordinates, length=len(self._coordinate_axes))
# 1 text column
self._text = toyplot.require.table_keys(table, text, length=1)
# 1 angle column
self._angle = toyplot.require.table_keys(table, angle, length=1)
# 1 fill color column
self._fill = toyplot.require.table_keys(table, fill, length=1)
# 1 opacity column
self._opacity = toyplot.require.table_keys(table, opacity, length=1)
# 1 title column
self._title = toyplot.require.table_keys(table, title, length=1)
# Text style
self._style = toyplot.style.combine(
{
"-toyplot-vertical-align": "middle",
"font-family": "helvetica",
"font-size": "12px",
"font-weight": "normal",
"stroke": "none",
"text-anchor": "middle",
},
toyplot.style.require(style, allowed=toyplot.style.allowed.text),
)
# Export filename
self._filename = toyplot.require.filename(filename)
def domain(self, axis):
for index, coordinate_axis in enumerate(self._coordinate_axes):
if coordinate_axis == axis:
return toyplot.data.minimax(self._table[self._coordinates[index]])
def extents(self, axes):
axis_map = {key: index for index, key in enumerate(self._coordinate_axes)}
coordinates = tuple([self._table[self._coordinates[axis_map[axis]]] for axis in axes])
extents = toyplot.text.extents(self._table[self._text[0]], self._table[self._angle[0]], self._style)
return coordinates, extents