0
votes

New in Python/Plotly, I'm trying to 'optimize' an interactive plot with plotly

below is df

Date    AA  AQ  AS  B6  CO  DH  DL  EV  F9  FL  HA  HP  MQ  NW  OH  OO  TZ  UA  US  WN  XE  YV  DayofMonth  Month
27/12   5.0 0.0 2.0 6.0 3.0 NaN 2.0 6.0 2.0 2.0 0.0 1.0 4.0 5.0 1.0 9.0 0.0 5.0 5.0 17.0    1.0 2.0 27  12
28/12   8.0 0.0 3.0 2.0 0.0 1.0 6.0 4.0 1.0 3.0 1.0 0.0 5.0 3.0 1.0 3.0 0.0 6.0 4.0 19.0    6.0 0.0 28  12
29/12   2.0 NaN 1.0 NaN 1.0 0.0 11.0    4.0 4.0 1.0 0.0 1.0 4.0 6.0 2.0 4.0 0.0 6.0 3.0 13.0    6.0 2.0 29  12




import plotly.graph_objects as go


# Initialize figure
fig = go.Figure()
df= pivot_gp.copy()

# Add Traces
for col in ['AA',   'AQ',   'AS',   'B6',   'CO',   'DH',   'DL',   'EV',   'F9',   'FL',   'HA',   'HP',   'MQ',   'NW',   'OH',   'OO',   'TZ',   'UA',   'US',   'WN',   'XE',   'YV']:
  fig.add_trace(go.Scatter(x= list(df.Date), y= list(df[col]), name= f"{col}", line= dict(color="red")))


fig.update_layout(updatemenus=[dict(active= 0, 
                                    buttons=list([dict(label= "None",
                                                       method= "update",
                                                       args= [{"visible": [False, False, False, False, False, False, False, False, False, False, False,
                                                                           False,False, False, False, False, False, False, False, False, False, False]}, {"title": "Unique Carriers"}]),

                                                  dict(label= "AA",
                                                       method= "update", 
                                                       args= [{"visible": [True, False, False, False, False, False, False, False, False, False, False,
                                                                           False,False, False, False, False, False, False, False, False, False, False]}, {"title": "Unique Carrier: AA"}]),

                                                  dict(label= "AQ", 
                                                       method= "update", 
                                                       args= [{"visible": [False, True, False, False, False, False, False, False, False, False, False,
                                                                           False,False, False, False, False, False, False, False, False, False, False]}, {"title": "Unique Carrier: AQ"}]), 

                                                  dict(label= "All", 
                                                       method= "update", 
                                                       args= [{"visible": [True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True]}, 
                                                             {"title": "Unique Carriers"}]),

                                                  ]), ) ])

As you can see, I've been able to create a loop to iterate over columns and add a line in the interactive plot. However, I'm having an issue for the update function.

Question:

How can I create a loop to iterate over all columns and add True when it's the right column for eg:

for col in [cols]:
     fig.update_layout(updatemenus=[dict(active= 0, 
                                            buttons=list([dict(label= f"{col}",
                                                               method= "update", 
                                                               args= [{"visible": [True, False, False, False, False, False, False, False, False, False, False,
                                                                                   False,False, False, False, False, False, False, False, False, False, False]}, {"title": "Unique Carrier: f'{col}'"}]),

The challenge is in the args list, while looping:

  • col 1 must be True then all False
  • col 2 False, True, then all False
  • col 3 False, False, True, then all False
  • and so on...

Bonus Question: Just to make it 'pretty', in the first loop how can I change color for each column in:

line= dict(color="red")

Thanks for anyone helping!

1

1 Answers

0
votes

Maybe it could help you:

  1. Replace

    for col in ['AA',   'AQ',   'AS',   'B6',   'CO',   'DH',   'DL',   'EV',   'F9',   'FL',   'HA',   'HP',   'MQ',   'NW',   'OH',   'OO',   'TZ',   'UA',   'US',   'WN',   'XE',   'YV']:
    

    with

    for col in df.columns:
    
  2. To change the visible list you can try:

    "visible":[ x==col for x in df.columns]
    
  3. For the color, you can set a dictionary:

    color_dict = { 'AA':"red", 'AQ':"blue", 'AS:"green"} 
    line = dict(color = color_dict.get(col))