Closed
Description
OS: OSX 10.13.2
Python: 3.6.4
I'm creating the following animated chart using the offline API.
Using 2.7.0 generating the HTML file (30MB) takes ~11 seconds (0m11.044s)
Using 3.0.0 generating the HTML file takes: 4 MINUTES (4m20.393s).
There is probably a much more efficient way of making this kind of animated scatter (please tell me if there is) but in 2.7 this was at least workable, in 3.0, not so much.
The following example reproduces the behavior. The size of the data is the same as in the gif above, the actual values are not.
import numpy as np
import plotly.graph_objs as go
import colorlover as cl
from plotly.offline import plot
sim_duration = 40.0
time_inc = 0.1
motor_unit_count = 120
times = np.arange(0.0, sim_duration, time_inc)
# Setting colors for plot.
potvin_scheme = [
'rgb(115, 0, 0)',
'rgb(252, 33, 23)',
'rgb(230, 185, 43)',
'rgb(107, 211, 100)',
'rgb(52, 211, 240)',
'rgb(36, 81, 252)',
'rgb(0, 6, 130)'
]
# It's hacky but also sorta cool.
c = cl.to_rgb(cl.interp(potvin_scheme, motor_unit_count))
c = [val.replace('rgb', 'rgba') for val in c]
c = [val.replace(')', ',{})') for val in c]
def get_color(trace_index: int) -> str:
# The first and every 20th trace should be full opacity
alpha = 0.2
if trace_index == 0 or ((trace_index + 1) % 20 == 0):
alpha = 1.0
color = c[trace_index].format(alpha)
return color
# Per Motor Unit Force
all_array = np.ones((120, len(times)))
data = []
annotations = []
anno_offsets = {
0: 20,
19: 30,
39: 40,
59: 45,
79: 17,
99: 56,
119: 170
}
max_y = np.amax(all_array)
for i, t in enumerate(all_array):
trace = go.Scatter(
x=times[:1],
y=t[:1],
name=i + 1,
marker=dict(
color=get_color(i)
)
)
data.append(trace)
frames = []
for i in range(1, len(times), 10):
frame_data = []
for j, t in enumerate(all_array):
trace = go.Scatter(
x=times[:i],
y=t[:i],
name=j + 1,
marker=dict(
color=get_color(j)
)
)
frame_data.append(trace)
frame = dict(
data=frame_data
)
frames.append(frame)
layout = go.Layout(
title='Motor Unit Forces by Time',
yaxis=dict(
title='Motor unit force (relative to MU1 tetanus)',
range=[0, max_y],
autorange=False
),
xaxis=dict(
title='Time (s)',
range=[0, sim_duration],
autorange=False
),
updatemenus=[{
'type': 'buttons',
'buttons': [{
'args': [
None,
{'frame': {'duration': 200, 'redraw': False},
'fromcurrent': True,
'transition': {'duration': 200, 'easing': 'linear'}
}
],
'label': 'Play',
'method': 'animate'
}]
}]
)
layout['annotations'] = annotations
fig = go.Figure(
data=data,
layout=layout,
frames=frames
)
plot(fig, filename='regression.html')
Metadata
Metadata
Assignees
Labels
No labels