Hello everyone,
Intro
I have an array of data frames that I would like to plot individually, but with with the ability to switch from one plot to another. I work with data that is separated by months. I got a large dataframe and created an arrays of data frames that where separated by months. In general I usually have 20 months, and would get complicated if done manually.
My question is I could use a replace this with a for loop?
Traditional Code:
trace1 = Scatter(
x=month1.Date, y=month1,
line=Line(
color='#FFD700',
width=3
),
name='Start_Date to End_Date'
)
trace2 = Scatter(
x=month2.Date, y=month2,
line=Line(
color='#C0C0C0',
width=3
),
name='Start_Date to End_Date'
)
trace3 = Scatter(
x=month3.Date, y=month3,
line=Line(
color='#BA8651',
width=3
),
name='Start_Date to End_Date'
)
trace4 = Scatter(
x=month4.Date, y=month4,
line=Line(
color='#000000',
width=3
),
name='Start_Date to End_Date'
)
data = Data([trace1, trace2, trace3, trace4])
layout = Layout(
title='Power Data Interval',
updatemenus=list([
dict(
x=-0.05,
y=1,
yanchor='top',
buttons=list([
dict(
args=['visible', [True, True, True, True]],
label='All',
method='restyle'
),
dict(
args=['visible', [True, False, False, False]],
label='Gold',
method='restyle'
),
dict(
args=['visible', [False, True, False, False]],
label='Silver',
method='restyle'
),
dict(
args=['visible', [False, False, True, False]],
label='Bronze',
method='restyle'
),
dict(
args=['visible', [False, False, False, True]],
label='Total',
method='restyle'
)
]),
)
]),
)
fig = Figure(data=data, layout=layout)
py.iplot(fig)
With this for loop code:
month = {} # array of dataframes by months
monthLength = len(month)
trace = {}
for i in range(0, monthLength, 1):
trace[i] = Scatter(
x=month[i].Date, y=month[i]['value'],
line=Line(
color='#FFD700',
width=3
),
name='Start_Date to End_Date') # The name of lot is begin and end date of that month Note: the name part is psuedu code but irrelevant
data = Data(trace)
layout = Layout(
title='Power Data Interval',
updatemenus=list([ # Not sure how to do update menus with a for loop I only need the race appear when it is clicked on.
dict(
x=-0.05,
y=1,
yanchor='top',
buttons=list([
dict(
args=['visible', [True, True, True, True]],
label='All',
method='restyle'
),
dict(
args=['visible', [True, False, False, False]],
label='month1',
method='restyle'
),
dict(
args=['visible', [False, True, False, False]],
label='month2',
method='restyle'
),
dict(
args=['visible', [False, False, True, False]],
label='month3',
method='restyle'
),
dict(
args=['month4', [False, False, False, True]],
label='Total',
method='restyle'
)
]),
)
]),
)
fig = Figure(data=data, layout=layout)
py.iplot(fig)
I want to automate the drop down list. I would like to know if this the correct approach, or if somebody knows another method.
Thank you for the help