IndexError: list index out of range when making a slider

I am working on a line graph that has a slider control. I am following the tutorial on the website and have come across a problem.

df_summed = df_pre_2003.groupby(['year']) ['nAllNeonic'].sum().reset_index()
traces=[]
q=df_summed
for i in range (0,len(q)):
    trace=dict(
        type='scatter',
        visible = False,
        line=dict(color='#00CED1', width=6),
        width=0.5,
        x = df_summed.year,
        y = df_summed.nAllNeonic
    )
traces[1129]['visible'] = True
steps=[]
for i in range(len(traces)):
    step=dict(
        method='restyle',
        args=['visible', [False] * len(data)],
    )
    step['args'][1][i] = True
    steps.append(step)
sliders = [dict(
    active = 10,
    currentvalue = {"prefix": "Year: "},
    #pad = {"t": 50},
    steps = steps
)]

layout = go.Layout(
    width=500,
    height=500,
    autosize=False,
    yaxis=dict(range=[0, 1])
)

layout['sliders'] = sliders
fig = go.Figure(data=traces, layout=layout)
#fig = dict(data=data, layout=layout)
pyo.iplot(fig, show_line=False)

This gives me an error:

IndexError: list index out of range

Specificlly at this line: traces[1129]['visible'] = True

Now I did some messing around like checking the index of df_pre_2003 like so, df_pre_2003.index.values
and recived an array that starts at 0 and goes until 1129.

So I changed traces to what is above and still doesn’t work.
The size of df_pre_2003 is 524 rows by 18 columns and the years are 1998 to 2003

My ultimate goal is to get a line graph with a slider like this Sliders in Python except instead of steps in the example it would be years for mine/ .

Hi @iiWylde,

From this code excerpt is looks like you’re not appending the traces you create in the for i in range(0, len(q)) loop to you traces list. I would take a look at the traces list before you do the property assignment to see if it has the traces in it that you expect.

Hope that helps!
-Jon

Thank you for the advice, I did forget that. I did that and now I get this,

ValueError:
Invalid element(s) received for the ‘steps’ property of layout.slider
Invalid elements include: [0 1991
1 1992
2 1993
3 1994
4 1995
5 1996
6 1997
7 1998
8 1999
9 2000
10 2001
11 2002
Name: year, dtype: int64]
The ‘steps’ property is a tuple of instances of
Step that may be specified as:

But, the thing is I dont know why. My updated code is:

df_summed = df_pre_2003.groupby(['year']) ['nAllNeonic'].sum().reset_index()
q=df_summed.year
r=df_summed.nAllNeonic
data = [dict(
        visible = False,
        #line=dict(color='#00CED1', width=6),
        #name = '𝜈 = '+str(step),
        x = q,
        y = r
)]
#data[0] seems off but not getting an index out of range error so leaving for now
data[0]['visible'] = True
steps=[]
for i in range (0,11,len(q)):
    steps.append(q)   
    step=dict(
        method = 'restyle',  
        args = ['visible', [False] * len(data)],
    )
    step['args'][1][i] = True # Toggle i'th trace to "visible"
    steps.append(step)
print(steps)
sliders=[dict(
    active = 11,
    currentvalue = {"prefix": "Year: "},
    #pad = {"t": 50},
    steps = steps
)]
layout = dict(sliders=sliders)
fig = dict(data=data, layout=layout)
plot(fig, filename='test slider')

I did some several prints like so,
print (data) provides:
[{‘visible’: False, ‘x’: 0 1991
1 1992
2 1993
3 1994
4 1995
5 1996
6 1997
7 1998
8 1999
9 2000
10 2001
11 2002

Name: year, dtype: int64, ‘y’: 0 0.0
1 0.0
2 0.0
3 11207.2
4 82134.0
5 101002.5
6 185898.7
7 166324.4
8 140227.8
9 133949.2
10 143690.4
11 168746.2
Name: nAllNeonic, dtype: float64}]
Which seems right.
And print(steps) provides:
[0 1991
1 1992
2 1993
3 1994
4 1995
5 1996
6 1997
7 1998
8 1999
9 2000
10 2001
11 2002
Name: year, dtype: int64, {‘method’: ‘restyle’, ‘args’: [‘visible’, [True]]}]

This all seems like it’s getting the right data needed but, is still giving an error. Thanks again.

Hi @iiWylde,

The layout.sliders.steps property needs to be a list of step objects. You can see the properties of a step object in the full reference at https://plot.ly/python/reference/#layout-sliders-steps. The elements of your Series containing years would become the step.label property.

Hope that helps a bit!
-Jon

1 Like