I’ve been trying to recreate plotly’s example from Gapminders dataset link (you may have to scroll down, sorry for that) and I am able to recreate it to some extent heres the code i.e. animation = working, buttons = working if and only if I remove the sliders part.
Non working part:(sliders added, Please note the working one does not have sliders)
import pandas as pd
import numpy as np
import plotly.offline as py
from plotly import graph_objs as go
py.init_notebook_mode(connect = False)
url = 'https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv'
dataset = pd.read_csv(url)
years = ['1952', '1962', '1967', '1972', '1977', '1982', '1987', '1992', '1997', '2002', '2007']
# make list of continents
continents = []
for continent in dataset['continent']:
if continent not in continents:
continents.append(continent)
year = 1952
data = []
for continent in continents:
dataset_by_year = dataset[dataset['year'] == year]
dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent]
trace = go.Scatter(
x = list(dataset_by_year_and_cont['lifeExp']),
y = list(dataset_by_year_and_cont['gdpPercap']),
text = list(dataset_by_year_and_cont['country']),
mode = 'markers',
marker = dict(
sizemode = 'area',
sizeref = 200000,
size = list(dataset_by_year_and_cont['pop'])
),
name = continent
)
data.append(trace)
layout = go.Layout(
title = 'Countries',
xaxis = dict(
range = [30,85],
title = 'Life Expectancy',
),
yaxis = dict(
title = 'GDP per Capita',
type = 'log'
),
hovermode = 'closest',
sliders = [dict(
visible = True,
active = 0,
yanchor = 'top',
xanchor = 'left',
currentvalue = dict(
font = {'size':20},
prefix = 'Year:',
visible = True,
xanchor = 'right'
),
transition = dict(
duration = 300,
easing = 'cubic-in-out'),
pad = {'b':10, 't': 50},
len = .9,
x = 0.1,
y = 0,
#updating parameter
steps = []
)],
updatemenus = [
dict(
buttons = [
dict(
args = [None, {'frame': {'duration': 500, 'redraw': False},
'fromcurrent': True, 'transition': {'duration': 300, 'easing': 'quadratic-in-out'}}],
label = 'Play',
method = 'animate'
),
dict(
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'
)
]
)
#making animation
frames_update = []
for year in years:
frames = {'data': [], 'name':str(year)}
for continent in continents:
dataset_by_year = dataset[dataset['year'] == int(year)]
dataset_by_year_and_cont = dataset_by_year[dataset_by_year['continent'] == continent]
trace = go.Scatter(
x = list(dataset_by_year_and_cont['lifeExp']),
y = list(dataset_by_year_and_cont['gdpPercap']),
text = list(dataset_by_year_and_cont['country']),
mode = 'markers',
marker = dict(
sizemode = 'area',
sizeref = 200000,
size = list(dataset_by_year_and_cont['pop'])
),
name = continent
)
frames['data'].append(trace)
frames_update.append(frames)
slider_step = dict(args = [
[year],
{'frame': {'duration': 300, 'redraw': False},
'mode': 'immediate',
'transition': {'duration': 300}}
],
label = year,
method = 'animate')
layout['sliders']['steps'].append(slider_step)
fig = go.Figure(data = data, frames = frames_update, layout = layout)
py.iplot(fig)
Please help! Thanks in advance.
##Edit 1: not able to access inner most functions e.g. layout['updatemenus']['buttons']
and layout['sliders']['steps']
any help on how to access could solve the problem.
##Edit 2: It works now, the major change that needed to be made was to add it as a list on sliders.