Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c225d6f

Browse files
committed
Merge pull request plotly#207 from plotly/extend_traces
Extend traces
2 parents 2d78a2b + cd630f2 commit c225d6f

File tree

3 files changed

+156
-10
lines changed

3 files changed

+156
-10
lines changed

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.6.13'
1+
__version__ = '1.6.14'

plotly/widgets/graphWidget.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plotly/widgets/graph_widget.py

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def plot(self, figure_or_data, validate=True):
308308
}
309309
self._handle_outgoing_message(message)
310310

311-
def restyle(self, data, indices=None):
311+
def restyle(self, update, indices=None):
312312
"""Update the style of existing traces in the Plotly graph.
313313
314314
Args:
@@ -346,10 +346,10 @@ def restyle(self, data, indices=None):
346346
Examples:
347347
Initialization - Start each example below with this setup:
348348
```
349-
from plotly.widgets import Graph
349+
from plotly.widgets import GraphWidget
350350
from IPython.display import display
351351
352-
graph = GraphWidget('https://plot.ly/~chris/3979')
352+
graph = GraphWidget()
353353
display(graph)
354354
```
355355
@@ -408,7 +408,11 @@ def restyle(self, data, indices=None):
408408
```
409409
"""
410410
# 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+
}
412416
if indices:
413417
message['indices'] = indices
414418
self._handle_outgoing_message(message)
@@ -435,7 +439,7 @@ def relayout(self, layout):
435439
Examples - Start each example below with this setup:
436440
Initialization:
437441
```
438-
from plotly.widgets import Graph
442+
from plotly.widgets import GraphWidget
439443
from IPython.display import display
440444
441445
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -518,7 +522,7 @@ def hover(self, *hover_objs):
518522
Examples:
519523
Initialization - Start each example below with this setup:
520524
```
521-
from plotly.widgets import Graph
525+
from plotly.widgets import GraphWidget
522526
from IPython.display import display
523527
524528
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -566,7 +570,7 @@ def add_traces(self, traces, new_indices=None):
566570
Examples:
567571
Initialization - Start each example below with this setup:
568572
```
569-
from plotly.widgets import Graph
573+
from plotly.widgets import GraphWidget
570574
from plotly.graph_objs import Scatter
571575
from IPython.display import display
572576
@@ -611,7 +615,7 @@ def delete_traces(self, indices):
611615
612616
Example - Delete the 2nd trace:
613617
```
614-
from plotly.widgets import Graph
618+
from plotly.widgets import GraphWidget
615619
from IPython.display import display
616620
617621
graph = GraphWidget('https://plot.ly/~chris/3979')
@@ -666,3 +670,145 @@ def reorder_traces(self, current_indices, new_indices=None):
666670
if new_indices is not None:
667671
message['newIndices'] = new_indices
668672
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

Comments
 (0)