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

Skip to content

Extend traces #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plotly/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.6.13'
__version__ = '1.6.14'
2 changes: 1 addition & 1 deletion plotly/widgets/graphWidget.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

162 changes: 154 additions & 8 deletions plotly/widgets/graph_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -346,10 +346,10 @@ def restyle(self, data, 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)
```

Expand Down Expand Up @@ -408,7 +408,11 @@ 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
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is weird spacing now:

message = {
    'task': 'restyle',
    'update': update,
    'graphId': self._graphId
}

?

if indices:
message['indices'] = indices
self._handle_outgoing_message(message)
Expand All @@ -435,7 +439,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')
Expand Down Expand Up @@ -518,7 +522,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')
Expand Down Expand Up @@ -566,7 +570,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

Expand Down Expand Up @@ -611,7 +615,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')
Expand Down Expand Up @@ -666,3 +670,145 @@ 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=(0,), 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 GraphWidget
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]]},
indices=[0])
```

Example 2 - Extend the second trace with x and y data
```
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]]
}, indices=[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)]
}, indices=[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]
},
indices=[0, 1],
max_points={
'x': [25, 50],
'y': [25, 50]
}
)
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice examples! in general, i think it'd be helpful to write indices=[0,1] where applicable so folks remember what the [0,1] is doing.

Also, you should have one example of extending with a '.' attribute, like marker.size.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

along with that, is it worth defaulting indices to be indices=(0,) or something?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's a good idea.


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',
'update': update,
'graphId': self._graphId,
'indices': indices
}
if max_points is not None:
message['maxPoints'] = max_points
self._handle_outgoing_message(message)