python - Plotly icreate_animations offline on Jupyter Notebook -
i trying replicate this plotly tutorial on jupyter notebook dataset matches 1 given in example, had change name of 1 column. issue here being offline command
py.icreate_animations(figure)
which in offline notebook mode becomes
icreate_animations(figure)
is not recognized. furthermore, if try iplot(figure) command, use static plots, long error raised:
--------------------------------------------------------------------------- attributeerror traceback (most recent call last) <ipython-input-27-b81a66612f63> in <module>() 38 figure['layout']['sliders'] = [sliders_dict] 39 ---> 40 iplot(figure) //anaconda/lib/python2.7/site-packages/plotly/offline/offline.pyc in iplot(figure_or_data, show_link, link_text, validate, image, filename, image_width, image_height, config) 330 config.setdefault('linktext', link_text) 331 --> 332 figure = tools.return_figure_from_figure_or_data(figure_or_data, validate) 333 334 # though can add quite bit display-bundle size, include //anaconda/lib/python2.7/site-packages/plotly/tools.pyc in return_figure_from_figure_or_data(figure_or_data, validate_figure) 1383 1384 try: -> 1385 graph_objs.figure(figure) 1386 except exceptions.plotlyerror err: 1387 raise exceptions.plotlyerror("invalid 'figure_or_data' argument. " //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 1159 1160 def __init__(self, *args, **kwargs): -> 1161 super(figure, self).__init__(*args, **kwargs) 1162 if 'data' not in self: 1163 self.data = data(_parent=self, _parent_key='data') //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 375 d = {key: val key, val in dict(*args, **kwargs).items()} 376 key, val in d.items(): --> 377 self.__setitem__(key, val, _raise=_raise) 378 379 def __dir__(self): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __setitem__(self, key, value, _raise) 430 431 if self._get_attribute_role(key) == 'object': --> 432 value = self._value_to_graph_object(key, value, _raise=_raise) 433 if not isinstance(value, (plotlydict, plotlylist)): 434 return //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in _value_to_graph_object(self, key, value, _raise) 541 # can `none` when `_raise == false` 542 return graphobjectfactory.create(key, value, _raise=_raise, --> 543 _parent=self, _parent_key=key) 544 545 def help(self, attribute=none, return_help=false): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in create(object_name, *args, **kwargs) 791 class_name = graph_reference.object_name_to_class_name.get(object_name) 792 if class_name in ['figure', 'data', 'frames']: --> 793 return globals()[class_name](*args, **kwargs) 794 else: 795 kwargs['_name'] = object_name //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 156 157 index, value in enumerate(list(*args)): --> 158 value = self._value_to_graph_object(index, value, _raise=_raise) 159 160 if isinstance(value, plotlybase): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in _value_to_graph_object(self, index, value, _raise) 1291 return value 1292 return super(frames, self)._value_to_graph_object(index, value, -> 1293 _raise=_raise) 1294 1295 def to_string(self, level=0, indent=4, eol='\n', //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in _value_to_graph_object(self, index, value, _raise) 221 return graphobjectfactory.create(item, _raise=_raise, 222 _parent=self, --> 223 _parent_key=index, **value) 224 except exceptions.plotlygraphobjecterror: 225 if == len(items) , _raise: //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in create(object_name, *args, **kwargs) 797 return plotlylist(*args, **kwargs) 798 else: --> 799 return plotlydict(*args, **kwargs) 800 801 //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 375 d = {key: val key, val in dict(*args, **kwargs).items()} 376 key, val in d.items(): --> 377 self.__setitem__(key, val, _raise=_raise) 378 379 def __dir__(self): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __setitem__(self, key, value, _raise) 430 431 if self._get_attribute_role(key) == 'object': --> 432 value = self._value_to_graph_object(key, value, _raise=_raise) 433 if not isinstance(value, (plotlydict, plotlylist)): 434 return //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in _value_to_graph_object(self, key, value, _raise) 541 # can `none` when `_raise == false` 542 return graphobjectfactory.create(key, value, _raise=_raise, --> 543 _parent=self, _parent_key=key) 544 545 def help(self, attribute=none, return_help=false): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in create(object_name, *args, **kwargs) 791 class_name = graph_reference.object_name_to_class_name.get(object_name) 792 if class_name in ['figure', 'data', 'frames']: --> 793 return globals()[class_name](*args, **kwargs) 794 else: 795 kwargs['_name'] = object_name //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 156 157 index, value in enumerate(list(*args)): --> 158 value = self._value_to_graph_object(index, value, _raise=_raise) 159 160 if isinstance(value, plotlybase): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in _value_to_graph_object(self, index, value, _raise) 1059 return graphobjectfactory.create(item, _raise=_raise, 1060 _parent=self, -> 1061 _parent_key=index, **value) 1062 1063 def get_data(self, flatten=false): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in create(object_name, *args, **kwargs) 797 return plotlylist(*args, **kwargs) 798 else: --> 799 return plotlydict(*args, **kwargs) 800 801 //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __init__(self, *args, **kwargs) 375 d = {key: val key, val in dict(*args, **kwargs).items()} 376 key, val in d.items(): --> 377 self.__setitem__(key, val, _raise=_raise) 378 379 def __dir__(self): //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs.pyc in __setitem__(self, key, value, _raise) 400 if key.endswith('src'): 401 if key in self._get_valid_attributes(): --> 402 value = graph_objs_tools.assign_id_to_src(key, value) 403 return super(plotlydict, self).__setitem__(key, value) 404 //anaconda/lib/python2.7/site-packages/plotly/graph_objs/graph_objs_tools.pyc in assign_id_to_src(src_name, src_value) 254 if src_id == '': 255 err = exceptions.column_not_yet_uploaded_message --> 256 err.format(column_name=src_value.name, reference=src_name) 257 raise exceptions.inputerror(err) 258 return src_id attributeerror: 'str' object has no attribute 'name'
so, know how make plot in offline mode?
edit: upload whole code:
import pandas pd import numpy np __future__ import division import plotly.plotly py plotly.offline import download_plotlyjs, init_notebook_mode, iplot plotly.graph_objs import * init_notebook_mode() plotly.grid_objs import grid, column plotly.tools import figurefactory ff dataset=pd.read_csv('gdp_mod_copia.csv') years_from_col = set(dataset['year']) years_ints = sorted(list(years_from_col)) years = [str(year) year in years_ints] # make list of continents continents = [] continent in dataset['continent']: if continent not in continents: continents.append(continent) columns = [] # make grid year in years: continent in continents: dataset_by_year = dataset[dataset['year'] == int(year)] dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent] col_name in dataset_by_year_and_cont: # each column name unique column_name = '{year}+{continent}+{header}_grid'.format( year=year, continent=continent, header=col_name ) a_column = column(list(dataset_by_year_and_cont[col_name]), column_name) columns.append(a_column) grid = grid(columns) figure = { 'data': [], 'layout': {}, 'frames': [], 'config': {'scrollzoom': true} } # fill in of layout figure['layout']['xaxis'] = {'range': [30, 85], 'title': 'life expectancy', 'gridcolor': '#ffffff'} figure['layout']['yaxis'] = {'title': 'gdp per capita', 'type': 'log', 'gridcolor': '#ffffff'} figure['layout']['hovermode'] = 'closest' figure['layout']['plot_bgcolor'] = 'rgb(223, 232, 243)' figure['layout']['slider'] = { 'args': [ 'slider.value', { 'duration': 400, 'ease': 'cubic-in-out' } ], 'initialvalue': '2007', 'plotlycommand': 'animate', 'values': years, 'visible': true } figure['layout']['updatemenus'] = [ { 'buttons': [ { 'args': [none, {'frame': {'duration': 500, 'redraw': false}, 'fromcurrent': true, 'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}], 'label': 'play', 'method': 'animate' }, { 'args': [[none], {'frame': {'duration': 0, 'redraw': false}, 'mode': 'immediate', 'transition': {'duration': 0}}], 'label': 'pause', 'method': 'animate' } ], 'direction': 'left', 'pad': {'r': 10, 't': 87}, 'showactive': false, 'type': 'buttons', 'x': 0.1, 'xanchor': 'right', 'y': 0, 'yanchor': 'top' } ] sliders_dict = { 'active': 0, 'yanchor': 'top', 'xanchor': 'left', 'currentvalue': { 'font': {'size': 20}, 'prefix': 'year:', 'visible': true, 'xanchor': 'right' }, 'transition': {'duration': 300, 'easing': 'cubic-in-out'}, 'pad': {'b': 10, 't': 50}, 'len': 0.9, 'x': 0.1, 'y': 0, 'steps': [] } custom_colors = { 'asia': 'rgb(171, 99, 250)', 'europe': 'rgb(230, 99, 250)', 'africa': 'rgb(99, 110, 250)', 'americas': 'rgb(25, 211, 243)', #'oceania': 'rgb(9, 255, 255)' 'oceania': 'rgb(50, 170, 255)' } col_name_template = '{year}+{continent}+{header}_grid' year = 2007 continent in continents: data_dict = { 'xsrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='gdp_per_capita' )), 'ysrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='life_satisfaction' )), 'mode': 'markers', 'textsrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='country' )), 'marker': { 'sizemode': 'area', 'sizeref': 200000, 'sizesrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='total_population' )), 'color': custom_colors[continent] }, 'name': continent } figure['data'].append(data_dict) year in years: frame = {'data': [], 'name': str(year)} continent in continents: data_dict = { 'xsrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='gdp_per_capita' )), 'ysrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='life_satisfaction' )), 'mode': 'markers', 'textsrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='country' )), 'marker': { 'sizemode': 'area', 'sizeref': 200000, 'sizesrc': grid.get_column_reference(col_name_template.format( year=year, continent=continent, header='total_population' )), 'color': custom_colors[continent] }, 'name': continent } frame['data'].append(data_dict) figure['frames'].append(frame) slider_step = {'args': [ [year], {'frame': {'duration': 300, 'redraw': false}, 'mode': 'immediate', 'transition': {'duration': 300}} ], 'label': year, 'method': 'animate'} sliders_dict['steps'].append(slider_step) figure['layout']['sliders'] = [sliders_dict] icreate_animations(figure)
and sample dataset:
,country,continent,year,country_code,total_population,life_satisfaction,gdp_per_capita 62,afghanistan,asia,2008,afg,29839994.0,3.723589897,1298.14315888 63,afghanistan,asia,2009,afg,30577756.0,4.401778221,1531.17399272 64,afghanistan,asia,2010,afg,31411743.0,4.75838089,1614.25500126 65,afghanistan,asia,2011,afg,32358260.0,3.83171916,1660.73985618 66,afghanistan,asia,2012,afg,33397058.0,3.782937527,1839.27357928 67,afghanistan,asia,2013,afg,34499915.0,3.572100401,1814.15582533 167,albania,europe,2007,alb,3169665.0,4.634251595,8447.88228539 169,albania,europe,2009,alb,3192723.0,5.485469818,9524.60981095 170,albania,europe,2010,alb,3204284.0,5.268936634,9927.13514733 171,albania,europe,2011,alb,3215988.0,5.867421627,10207.7006745 172,albania,europe,2012,alb,3227373.0,5.510124207,10369.7616592 173,albania,europe,2013,alb,3238316.0,4.550647736,10504.0930888 242,algeria,africa,2010,dza,35468208.0,5.46356678,12870.2162376 243,algeria,africa,2011,dza,35980193.0,5.317194462,12989.9549601 244,algeria,africa,2012,dza,36485828.0,5.604595661,13161.566464 451,angola,africa,2011,ago,19618432.0,5.589000702,5911.25433387 452,angola,africa,2012,ago,20162517.0,4.360249996,5998.63860099
so lets start issues in code.
you using
from plotly.grid_objs import grid, column
make graph functionality of plotly online, have substituted logic grid simple dataframe, same thing.second, noticed graph not displaying data, after analyzing found out had set range x-axis
[30, 85]
, x-axis values between1-15k
.
please find below updated code. hope solves issue.
code:
import pandas pd import numpy np __future__ import division import plotly.plotly py plotly.offline import download_plotlyjs, init_notebook_mode, iplot plotly.graph_objs import * init_notebook_mode() plotly.grid_objs import grid, column plotly.tools import figurefactory ff dataset=pd.read_csv('god_mod_copia.csv') years_from_col = set(dataset['year']) years_ints = sorted(list(years_from_col)) years = [str(year) year in years_ints] # make list of continents continents = [] continent in dataset['continent']: if continent not in continents: continents.append(continent) df = pd.dataframe() # make grid year in years: continent in continents: dataset_by_year = dataset[dataset['year'] == int(year)] dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent] col_name in dataset_by_year_and_cont: # each column name unique temp = '{year}+{continent}+{header}_grid'.format( year=year, continent=continent, header=col_name ) #if dataset_by_year_and_cont[col_name].size != 0: df = df.append({'value': list(dataset_by_year_and_cont[col_name]), 'key': temp}, ignore_index=true) figure = { 'data': [], 'layout': {}, 'frames': [] } figure['layout']['xaxis'] = {'title': 'gdp per capita', 'type': 'log', 'autorange': true} #was not set figure['layout']['yaxis'] = {'title': 'life expectancy', 'autorange': true} #was not set figure['layout']['hovermode'] = 'closest' figure['layout']['showlegend'] = true figure['layout']['sliders'] = { 'args': [ 'slider.value', { 'duration': 400, 'ease': 'cubic-in-out' } ], 'initialvalue': '2007', 'plotlycommand': 'animate', 'values': years, 'visible': true } figure['layout']['updatemenus'] = [ { 'buttons': [ { 'args': [none, {'frame': {'duration': 500, 'redraw': false}, 'fromcurrent': true, 'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}], 'label': 'play', 'method': 'animate' }, { 'args': [[none], {'frame': {'duration': 0, 'redraw': false}, 'mode': 'immediate', 'transition': {'duration': 0}}], 'label': 'pause', 'method': 'animate' } ], 'direction': 'left', 'pad': {'r': 10, 't': 87}, 'showactive': false, 'type': 'buttons', 'x': 0.1, 'xanchor': 'right', 'y': 0, 'yanchor': 'top' } ] sliders_dict = { 'active': 0, 'yanchor': 'top', 'xanchor': 'left', 'currentvalue': { 'font': {'size': 20}, 'prefix': 'year:', 'visible': true, 'xanchor': 'right' }, 'transition': {'duration': 300, 'easing': 'cubic-in-out'}, 'pad': {'b': 10, 't': 50}, 'len': 0.9, 'x': 0.1, 'y': 0, 'steps': [] } custom_colors = { 'asia': 'rgb(171, 99, 250)', 'europe': 'rgb(230, 99, 250)', 'africa': 'rgb(99, 110, 250)', 'americas': 'rgb(25, 211, 243)', #'oceania': 'rgb(9, 255, 255)' 'oceania': 'rgb(50, 170, 255)' } col_name_template = '{year}+{continent}+{header}_grid' year = 2007 continent in continents: data_dict = { 'x': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='gdp_per_capita' ), 'value'].values[0], 'y': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='life_satisfaction' ), 'value'].values[0], 'mode': 'markers', 'text': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='country' ), 'value'].values[0], 'marker': { 'sizemode': 'area', 'sizeref': 200000, 'size': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='total_population' ), 'value'].values[0], 'color': custom_colors[continent] }, 'name': continent } figure['data'].append(data_dict) year in years: frame = {'data': [], 'name': str(year)} continent in continents: data_dict = { 'x': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='gdp_per_capita' ), 'value'].values[0], 'y': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='life_satisfaction' ), 'value'].values[0], 'mode': 'markers', 'text': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='country' ), 'value'].values[0], 'marker': { 'sizemode': 'area', 'sizeref': 200000, 'size': df.loc[df['key']==col_name_template.format( year=year, continent=continent, header='total_population' ), 'value'].values[0], 'color': custom_colors[continent] }, 'name': continent } frame['data'].append(data_dict) figure['frames'].append(frame) #this block indented , should not have been. slider_step = {'args': [ [year], {'frame': {'duration': 300, 'redraw': false}, 'mode': 'immediate', 'transition': {'duration': 300}} ], 'label': year, 'method': 'animate'} sliders_dict['steps'].append(slider_step) figure['layout']['sliders'] = [sliders_dict] iplot(figure, config={'scrollzoom': true})
Comments
Post a Comment