-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotlySetup.py
67 lines (55 loc) · 1.72 KB
/
plotlySetup.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
import plotly.plotly as py
import plotly.tools as tls
import plotly.graph_objs as go
import time
def setupPlotly(points):
'''
Sets up plotly graph to write to
:param points: <int> of max number of points to keep plotted on graph
:return: Plotly Stream object
'''
# Setup plotly
stream_ids = tls.get_credentials_file()['stream_ids']
# Get stream id from stream id list
stream_id = stream_ids[0]
# Make instance of stream id object
stream_1 = go.Stream(
token=stream_id, # link stream id to 'token' key
maxpoints=points # keep a max of 80 pts on screen
)
# Initialize trace of streaming plot by embedding the unique stream_id
trace1 = go.Scatter(
x=[],
y=[],
mode='lines',
stream=stream_1 # (!) embed stream id, 1 per trace
)
data = go.Data([trace1])
# Add title to layout object
layout = go.Layout(
title='Twitter Overwatch Sentiment',
xaxis=dict(
title='Date & Time',
titlefont=dict(
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Aggregate Sentiment',
titlefont=dict(
size=18,
color='#7f7f7f'
)
)
)
# Make a figure object
fig = go.Figure(data=data, layout=layout)
# Send fig to Plotly, initialize streaming plot, open new tab
py.iplot(fig, filename='python-streaming')
# We will provide the stream link object the same token that's associated with the trace we wish to stream to
stream_object = py.Stream(stream_id)
# We then open a connection
stream_object.open()
time.sleep(5)
return stream_object