From ad75ebbe198dc559dc4a2124218c515f1e1b47be Mon Sep 17 00:00:00 2001 From: Chris P Date: Sun, 22 Mar 2015 18:51:37 -0400 Subject: [PATCH 01/11] :lipstick: --- plotly/widgets/graphWidget.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/widgets/graphWidget.js b/plotly/widgets/graphWidget.js index 5848ebdbf03..e1ba64c2a86 100644 --- a/plotly/widgets/graphWidget.js +++ b/plotly/widgets/graphWidget.js @@ -14,7 +14,7 @@ require(["widgets/js/widget", "widgets/js/manager"], function (widget, manager) // construct the expected IPython3 widget API IPYTHON_VERSION = '2'; manager = {WidgetManager: widget}; - widget = {DOMWidgetView: IPython.DOMWidgetView} + widget = {DOMWidgetView: IPython.DOMWidgetView}; } var GraphView = widget.DOMWidgetView.extend({ From 5c5469f4d0afed55e5a7707e13936a374b1661a7 Mon Sep 17 00:00:00 2001 From: Chris P Date: Sun, 22 Mar 2015 18:52:34 -0400 Subject: [PATCH 02/11] use update instead of data argument --- plotly/widgets/graph_widget.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 89e22769170..e7ced4626ff 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -308,7 +308,7 @@ def plot(self, figure_or_data, validate=True): } self._handle_outgoing_message(message) - def restyle(self, data, indices=None): + def restyle(self, update, indices=None): """Update the style of existing traces in the Plotly graph. Args: @@ -408,7 +408,10 @@ def restyle(self, data, indices=None): ``` """ # TODO: Add flat traces to graph_objs - message = {'task': 'restyle', 'update': data, 'graphId': self._graphId} + message = { + 'task': 'restyle', 'update': update, + 'graphId': self._graphId + } if indices: message['indices'] = indices self._handle_outgoing_message(message) From d5696af5464dc0d45491ee4574b58372d97a498d Mon Sep 17 00:00:00 2001 From: Chris P Date: Sun, 22 Mar 2015 18:53:15 -0400 Subject: [PATCH 03/11] extend functionality --- plotly/widgets/graph_widget.py | 102 +++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index e7ced4626ff..429733b7d49 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -669,3 +669,105 @@ def reorder_traces(self, current_indices, new_indices=None): if new_indices is not None: message['newIndices'] = new_indices self._handle_outgoing_message(message) + + def extend_traces(self, update, indices, max_points=None): + """ Append data points to existing traces in the Plotly graph. + + Args: + update (dict): + dict where keys are the graph attribute strings + and values are arrays of arrays with values to extend. + + Each array in the array will extend a trace. + + Valid keys include: + 'x', 'y', 'text, + 'marker.color', 'marker.size', 'marker.symbol', + 'marker.line.color', 'marker.line.width' + + indices (list, int): + Specify which traces to apply the `update` dict to. + If indices are not given, the update will apply to + the traces in order. + + max_points (int or dict, optional): + If specified, then only show the `max_points` most + recent points in the graph. + This is useful to prevent traces from becoming too + large (and slow) or for creating "windowed" graphs + in monitoring applications. + + To set max_points to different values for each trace + or attribute, set max_points to a dict mapping keys + to max_points values. See the examples below. + + Examples: + Initialization - Start each example below with this setup: + ``` + from plotly.widgets import Graph + from IPython.display import display + + graph = GraphWidget() + graph.plot([ + {'x': [], 'y': []}, + {'x': [], 'y': []} + ]) + + display(graph) + ``` + + Example 1 - Extend the first trace with x and y data + ``` + graph.extend_traces({'x': [[1,2,3]], 'y': [[10,20,30]]}, [0]) + ``` + + Example 2 - Extend the second trace with x and y data + ``` + graph.extend_traces({'x': [[1,2,3]], 'y': [[10,20,30]]}, [1]) + ``` + + Example 3 - Extend the first two traces with x and y data + ``` + graph.extend_traces({ + 'x': [[1,2,3], [2,3,4]], + 'y': [[10,20,30], [3,4,3]] + }, [0, 1]) + ``` + + Example 4 - Extend the first trace with x and y data and + limit the length of data in that trace to 50 + points. + ``` + + graph.extend_traces({ + 'x': [range(100)], + 'y': [range(100)] + }, [0, 1], max_points=50) + ``` + + Example 5 - Extend the first and second trace with x and y data + and limit the length of data in the first trace to + 25 points and the second trace to 50 points. + ``` + new_points = range(100) + graph.extend_traces({ + 'x': [new_points, new_points], + 'y': [new_points, new_points] + }, + [0, 1], + max_points={ + 'x': [25, 50], + 'y': [25, 50] + } + ) + ``` + """ + message = { + 'task': 'extendTraces', + 'update': update, + 'graphId': self._graphId, + 'indices': indices + } + if max_points is not None: + message['maxPoints'] = max_points + self._handle_outgoing_message(message) From 042dfbcdcae4f5fd2051c976502b09debded3337 Mon Sep 17 00:00:00 2001 From: Chris P Date: Sun, 22 Mar 2015 19:30:37 -0400 Subject: [PATCH 04/11] 1.6.14: Extend api --- plotly/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/version.py b/plotly/version.py index dc96272cff6..a4ef8b7036a 100644 --- a/plotly/version.py +++ b/plotly/version.py @@ -1 +1 @@ -__version__ = '1.6.13' +__version__ = '1.6.14' From 8a61494ff875712551a05aff286f14d6a5c6ad42 Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:19:21 -0400 Subject: [PATCH 05/11] GraphWidget, not Graph --- plotly/widgets/graph_widget.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 429733b7d49..1891d967f9e 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -346,10 +346,10 @@ def restyle(self, update, indices=None): Examples: Initialization - Start each example below with this setup: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from IPython.display import display - graph = GraphWidget('https://plot.ly/~chris/3979') + graph = GraphWidget() display(graph) ``` @@ -438,7 +438,7 @@ def relayout(self, layout): Examples - Start each example below with this setup: Initialization: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from IPython.display import display graph = GraphWidget('https://plot.ly/~chris/3979') @@ -521,7 +521,7 @@ def hover(self, *hover_objs): Examples: Initialization - Start each example below with this setup: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from IPython.display import display graph = GraphWidget('https://plot.ly/~chris/3979') @@ -569,7 +569,7 @@ def add_traces(self, traces, new_indices=None): Examples: Initialization - Start each example below with this setup: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from plotly.graph_objs import Scatter from IPython.display import display @@ -614,7 +614,7 @@ def delete_traces(self, indices): Example - Delete the 2nd trace: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from IPython.display import display graph = GraphWidget('https://plot.ly/~chris/3979') @@ -704,7 +704,7 @@ def extend_traces(self, update, indices, max_points=None): Examples: Initialization - Start each example below with this setup: ``` - from plotly.widgets import Graph + from plotly.widgets import GraphWidget from IPython.display import display graph = GraphWidget() From eb24be1b710ebb016139f31d4df5d18e446f1b11 Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:19:37 -0400 Subject: [PATCH 06/11] :lipstick: --- plotly/widgets/graph_widget.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 1891d967f9e..fee7f80f154 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -409,7 +409,8 @@ def restyle(self, update, indices=None): """ # TODO: Add flat traces to graph_objs message = { - 'task': 'restyle', 'update': update, + 'task': 'restyle', + 'update': update, 'graphId': self._graphId } if indices: From 99faf0c5fbbe8b84cfbb75179f975a2bca0b0aba Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:19:49 -0400 Subject: [PATCH 07/11] specify indices --- plotly/widgets/graph_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index fee7f80f154..0bdf4e5cab0 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -671,7 +671,7 @@ def reorder_traces(self, current_indices, new_indices=None): message['newIndices'] = new_indices self._handle_outgoing_message(message) - def extend_traces(self, update, indices, max_points=None): + def extend_traces(self, update, indices=(0,), max_points=None): """ Append data points to existing traces in the Plotly graph. Args: From 38b40fd52dcf6059b58b71d44028d4d2568ef3ea Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:22:36 -0400 Subject: [PATCH 08/11] name indices argument in examples --- plotly/widgets/graph_widget.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 0bdf4e5cab0..a7d771d2c43 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -719,20 +719,22 @@ def extend_traces(self, update, indices=(0,), max_points=None): Example 1 - Extend the first trace with x and y data ``` - graph.extend_traces({'x': [[1,2,3]], 'y': [[10,20,30]]}, [0]) + graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]}, + indices=[0]) ``` Example 2 - Extend the second trace with x and y data ``` - graph.extend_traces({'x': [[1,2,3]], 'y': [[10,20,30]]}, [1]) + graph.extend_traces({'x': [[1, 2, 3]], 'y': [[10, 20, 30]]}, + indices=[1]) ``` Example 3 - Extend the first two traces with x and y data ``` graph.extend_traces({ - 'x': [[1,2,3], [2,3,4]], - 'y': [[10,20,30], [3,4,3]] - }, [0, 1]) + 'x': [[1, 2, 3], [2, 3, 4]], + 'y': [[10, 20, 30], [3, 4, 3]] + }, indices=[0, 1]) ``` Example 4 - Extend the first trace with x and y data and @@ -743,7 +745,7 @@ def extend_traces(self, update, indices=(0,), max_points=None): graph.extend_traces({ 'x': [range(100)], 'y': [range(100)] - }, [0, 1], max_points=50) + }, indices=[0, 1], max_points=50) ``` Example 5 - Extend the first and second trace with x and y data @@ -755,6 +757,7 @@ def extend_traces(self, update, indices=(0,), max_points=None): 'x': [new_points, new_points], 'y': [new_points, new_points] }, + indices=[0, 1], [0, 1], max_points={ 'x': [25, 50], From 024b9f54f95dcc3372416a5db24241226a75235a Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:22:50 -0400 Subject: [PATCH 09/11] :lipstick: --- plotly/widgets/graph_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index a7d771d2c43..85751966eae 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -733,7 +733,7 @@ def extend_traces(self, update, indices=(0,), max_points=None): ``` graph.extend_traces({ 'x': [[1, 2, 3], [2, 3, 4]], - 'y': [[10, 20, 30], [3, 4, 3]] + 'y': [[10, 20, 30], [3, 4, 3]] }, indices=[0, 1]) ``` From 190a9c74ffa79982ea5bb64d110cfed03d13ca3f Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:23:02 -0400 Subject: [PATCH 10/11] :lipstick: --- plotly/widgets/graph_widget.py | 1 - 1 file changed, 1 deletion(-) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index 85751966eae..c246654b769 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -758,7 +758,6 @@ def extend_traces(self, update, indices=(0,), max_points=None): 'y': [new_points, new_points] }, indices=[0, 1], - [0, 1], max_points={ 'x': [25, 50], 'y': [25, 50] From cd630f2cccbc767d5be97a4fce08d8ed0c414b2a Mon Sep 17 00:00:00 2001 From: Chris P Date: Thu, 26 Mar 2015 21:23:07 -0400 Subject: [PATCH 11/11] new examples --- plotly/widgets/graph_widget.py | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/plotly/widgets/graph_widget.py b/plotly/widgets/graph_widget.py index c246654b769..be676921d2a 100644 --- a/plotly/widgets/graph_widget.py +++ b/plotly/widgets/graph_widget.py @@ -764,6 +764,44 @@ def extend_traces(self, update, indices=(0,), max_points=None): } ) ``` + + Example 6 - Update other attributes, like marker colors and + sizes and text + ``` + # Initialize a plot with some empty attributes + graph.plot([{ + 'x': [], + 'y': [], + 'text': [], + 'marker': { + 'size': [], + 'color': [] + } + }]) + # Append some data into those attributes + graph.extend_traces({ + 'x': [[1, 2, 3]], + 'y': [[10, 20, 30]], + 'text': [['A', 'B', 'C']], + 'marker.size': [[10, 15, 20]], + 'marker.color': [['blue', 'red', 'orange']] + }, indices=[0]) + ``` + + Example 7 - Live-update a graph over a few seconds + ``` + import time + + graph.plot([{'x': [], 'y': []}]) + for i in range(10): + graph.extend_traces({ + 'x': [[i]], + 'y': [[i]] + }, indices=[0]) + + time.sleep(0.5) + ``` + """ message = { 'task': 'extendTraces',