@@ -308,7 +308,7 @@ def plot(self, figure_or_data, validate=True):
308
308
}
309
309
self ._handle_outgoing_message (message )
310
310
311
- def restyle (self , data , indices = None ):
311
+ def restyle (self , update , indices = None ):
312
312
"""Update the style of existing traces in the Plotly graph.
313
313
314
314
Args:
@@ -346,10 +346,10 @@ def restyle(self, data, indices=None):
346
346
Examples:
347
347
Initialization - Start each example below with this setup:
348
348
```
349
- from plotly.widgets import Graph
349
+ from plotly.widgets import GraphWidget
350
350
from IPython.display import display
351
351
352
- graph = GraphWidget('https://plot.ly/~chris/3979' )
352
+ graph = GraphWidget()
353
353
display(graph)
354
354
```
355
355
@@ -408,7 +408,11 @@ def restyle(self, data, indices=None):
408
408
```
409
409
"""
410
410
# TODO: Add flat traces to graph_objs
411
- message = {'task' : 'restyle' , 'update' : data , 'graphId' : self ._graphId }
411
+ message = {
412
+ 'task' : 'restyle' ,
413
+ 'update' : update ,
414
+ 'graphId' : self ._graphId
415
+ }
412
416
if indices :
413
417
message ['indices' ] = indices
414
418
self ._handle_outgoing_message (message )
@@ -435,7 +439,7 @@ def relayout(self, layout):
435
439
Examples - Start each example below with this setup:
436
440
Initialization:
437
441
```
438
- from plotly.widgets import Graph
442
+ from plotly.widgets import GraphWidget
439
443
from IPython.display import display
440
444
441
445
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -518,7 +522,7 @@ def hover(self, *hover_objs):
518
522
Examples:
519
523
Initialization - Start each example below with this setup:
520
524
```
521
- from plotly.widgets import Graph
525
+ from plotly.widgets import GraphWidget
522
526
from IPython.display import display
523
527
524
528
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -566,7 +570,7 @@ def add_traces(self, traces, new_indices=None):
566
570
Examples:
567
571
Initialization - Start each example below with this setup:
568
572
```
569
- from plotly.widgets import Graph
573
+ from plotly.widgets import GraphWidget
570
574
from plotly.graph_objs import Scatter
571
575
from IPython.display import display
572
576
@@ -611,7 +615,7 @@ def delete_traces(self, indices):
611
615
612
616
Example - Delete the 2nd trace:
613
617
```
614
- from plotly.widgets import Graph
618
+ from plotly.widgets import GraphWidget
615
619
from IPython.display import display
616
620
617
621
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -666,3 +670,145 @@ def reorder_traces(self, current_indices, new_indices=None):
666
670
if new_indices is not None :
667
671
message ['newIndices' ] = new_indices
668
672
self ._handle_outgoing_message (message )
673
+
674
+ def extend_traces (self , update , indices = (0 ,), max_points = None ):
675
+ """ Append data points to existing traces in the Plotly graph.
676
+
677
+ Args:
678
+ update (dict):
679
+ dict where keys are the graph attribute strings
680
+ and values are arrays of arrays with values to extend.
681
+
682
+ Each array in the array will extend a trace.
683
+
684
+ Valid keys include:
685
+ 'x', 'y', 'text,
686
+ 'marker.color', 'marker.size', 'marker.symbol',
687
+ 'marker.line.color', 'marker.line.width'
688
+
689
+ indices (list, int):
690
+ Specify which traces to apply the `update` dict to.
691
+ If indices are not given, the update will apply to
692
+ the traces in order.
693
+
694
+ max_points (int or dict, optional):
695
+ If specified, then only show the `max_points` most
696
+ recent points in the graph.
697
+ This is useful to prevent traces from becoming too
698
+ large (and slow) or for creating "windowed" graphs
699
+ in monitoring applications.
700
+
701
+ To set max_points to different values for each trace
702
+ or attribute, set max_points to a dict mapping keys
703
+ to max_points values. See the examples below.
704
+
705
+ Examples:
706
+ Initialization - Start each example below with this setup:
707
+ ```
708
+ from plotly.widgets import GraphWidget
709
+ from IPython.display import display
710
+
711
+ graph = GraphWidget()
712
+ graph.plot([
713
+ {'x': [], 'y': []},
714
+ {'x': [], 'y': []}
715
+ ])
716
+
717
+ display(graph)
718
+ ```
719
+
720
+ Example 1 - Extend the first trace with x and y data
721
+ ```
722
+ graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]},
723
+ indices=[0])
724
+ ```
725
+
726
+ Example 2 - Extend the second trace with x and y data
727
+ ```
728
+ graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]},
729
+ indices=[1])
730
+ ```
731
+
732
+ Example 3 - Extend the first two traces with x and y data
733
+ ```
734
+ graph.extend_traces({
735
+ 'x': [[1, 2, 3], [2, 3, 4]],
736
+ 'y': [[10, 20, 30], [3, 4, 3]]
737
+ }, indices=[0, 1])
738
+ ```
739
+
740
+ Example 4 - Extend the first trace with x and y data and
741
+ limit the length of data in that trace to 50
742
+ points.
743
+ ```
744
+
745
+ graph.extend_traces({
746
+ 'x': [range(100)],
747
+ 'y': [range(100)]
748
+ }, indices=[0, 1], max_points=50)
749
+ ```
750
+
751
+ Example 5 - Extend the first and second trace with x and y data
752
+ and limit the length of data in the first trace to
753
+ 25 points and the second trace to 50 points.
754
+ ```
755
+ new_points = range(100)
756
+ graph.extend_traces({
757
+ 'x': [new_points, new_points],
758
+ 'y': [new_points, new_points]
759
+ },
760
+ indices=[0, 1],
761
+ max_points={
762
+ 'x': [25, 50],
763
+ 'y': [25, 50]
764
+ }
765
+ )
766
+ ```
767
+
768
+ Example 6 - Update other attributes, like marker colors and
769
+ sizes and text
770
+ ```
771
+ # Initialize a plot with some empty attributes
772
+ graph.plot([{
773
+ 'x': [],
774
+ 'y': [],
775
+ 'text': [],
776
+ 'marker': {
777
+ 'size': [],
778
+ 'color': []
779
+ }
780
+ }])
781
+ # Append some data into those attributes
782
+ graph.extend_traces({
783
+ 'x': [[1, 2, 3]],
784
+ 'y': [[10, 20, 30]],
785
+ 'text': [['A', 'B', 'C']],
786
+ 'marker.size': [[10, 15, 20]],
787
+ 'marker.color': [['blue', 'red', 'orange']]
788
+ }, indices=[0])
789
+ ```
790
+
791
+ Example 7 - Live-update a graph over a few seconds
792
+ ```
793
+ import time
794
+
795
+ graph.plot([{'x': [], 'y': []}])
796
+ for i in range(10):
797
+ graph.extend_traces({
798
+ 'x': [[i]],
799
+ 'y': [[i]]
800
+ }, indices=[0])
801
+
802
+ time.sleep(0.5)
803
+ ```
804
+
805
+ """
806
+ message = {
807
+ 'task' : 'extendTraces' ,
808
+ 'update' : update ,
809
+ 'graphId' : self ._graphId ,
810
+ 'indices' : indices
811
+ }
812
+ if max_points is not None :
813
+ message ['maxPoints' ] = max_points
814
+ self ._handle_outgoing_message (message )
0 commit comments