import plotly.plotly as py
from plotly.graph_objs import *
import numpy as np
def print_trace_line(fig):
for trace in fig['data']:
print trace['line']
return
# Make Data object,
# made up 20 Scatter objects plotting 50 norm. dist. random pts
data = Data([
Scatter(
x=range(50),
y=np.random.randn(50)
)
for i in range(20) # N.B. line breaks and indents are just for aesthetics
])
fig = Figure() # init. Figure object
fig['data'] = data # add data
fig['layout'] = Layout(showlegend=False) # remove legend from plot
# Make a dictionary linking the 'line' key to a Line object
style = dict(
line=Line(
color='blue',
width=0.3,
opacity=0.7
)
)
# Update each trace linked to 'data'!
fig['data'].update(style)
print_trace_line(fig)
{'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'} {'opacity': 0.7, 'width': 0.3, 'color': 'blue'}
This works as desired.
The problems start here:
# Make a dictionary linking the 'line' key to a Line object
style1 = dict(
color='red',
width=1,
opacity=1
)
# Update 1st trace item linked to 'data' key in figure object
fig['data'][0]['line'].update(style1)
print_trace_line(fig)
{'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'} {'opacity': 1, 'width': 1, 'color': 'red'}
Hold on. All traces linked to 'data'
are updated.
In addition:
# Make color map (using colorbrewer 'Reds')
colors = ['rgb(254,229,217)','rgb(252,174,145)','rgb(251, 106, 74)',
'rgb(222,45,38)','rgb(165,15,21)']
# Make list of 5 dictionaries, sent update to 'data' key in figure object,
# iteration is repeated 4 times over the trace object list
styles = [dict(
line=Line(
color=colors[i],
width=0.5,
opacity=0.7
)
) for i in range(5)]
# Update!
fig['data'].update(styles)
print_trace_line(fig)
{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'} {'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}
Wait. Looks all traces were updated in with each element in styles
(as the final result correspond to colors[-1]
).
from IPython.display import display, HTML
import urllib2
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
display(HTML(urllib2.urlopen(url).read()))