{
"metadata": {
"name": "",
"signature": "sha256:e9f2386c8459fd0c54c8d65a10ef39151da02277c4cb15aacf88079c3c66e3de"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import plotly.plotly as py\n",
"from plotly.graph_objs import *"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np "
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def print_trace_line(fig):\n",
" for trace in fig['data']:\n",
" print trace['line']\n",
" return"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Make Data object, \n",
"# made up 20 Scatter objects plotting 50 norm. dist. random pts\n",
"data = Data([\n",
" Scatter(\n",
" x=range(50), \n",
" y=np.random.randn(50)\n",
" ) \n",
" for i in range(20) # N.B. line breaks and indents are just for aesthetics\n",
"])\n",
"\n",
"fig = Figure() # init. Figure object \n",
"fig['data'] = data # add data\n",
"fig['layout'] = Layout(showlegend=False) # remove legend from plot"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Make a dictionary linking the 'line' key to a Line object\n",
"style = dict(\n",
" line=Line(\n",
" color='blue', \n",
" width=0.3, \n",
" opacity=0.7\n",
" )\n",
")\n",
"\n",
"# Update each trace linked to 'data'!\n",
"fig['data'].update(style)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print_trace_line(fig)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n",
"{'opacity': 0.7, 'width': 0.3, 'color': 'blue'}\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This works as desired.\n",
"\n",
"The problems start here:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Make a dictionary linking the 'line' key to a Line object\n",
"style1 = dict(\n",
" color='red', \n",
" width=1, \n",
" opacity=1\n",
")\n",
"\n",
"# Update 1st trace item linked to 'data' key in figure object\n",
"fig['data'][0]['line'].update(style1)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print_trace_line(fig)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n",
"{'opacity': 1, 'width': 1, 'color': 'red'}\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Hold on. All traces linked to `'data'` are updated. \n",
"\n",
"In addition:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Make color map (using colorbrewer 'Reds')\n",
"colors = ['rgb(254,229,217)','rgb(252,174,145)','rgb(251, 106, 74)',\n",
" 'rgb(222,45,38)','rgb(165,15,21)']\n",
"\n",
"# Make list of 5 dictionaries, sent update to 'data' key in figure object,\n",
"# iteration is repeated 4 times over the trace object list\n",
"styles = [dict(\n",
" line=Line(\n",
" color=colors[i], \n",
" width=0.5, \n",
" opacity=0.7\n",
" )\n",
") for i in range(5)]\n",
"\n",
"# Update!\n",
"fig['data'].update(styles)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print_trace_line(fig)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n",
"{'opacity': 0.7, 'width': 0.5, 'color': 'rgb(165,15,21)'}\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Wait. Looks all traces were updated in with each element in `styles` (as the final result correspond to `colors[-1]`)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
\n",
"