'Efficient way to stack data in plotly

I´ve got the same structured monthly data twelve times. Now I want to add every dataset into a linechart. Of course, one way would be to add every single set themselves. As they are all structured the same, I wonder if theres a more efficient way? Thanks for your help!

flights_jan = january.groupby("DAY")["YEAR"].count()
flights_feb = february.groupby("DAY")["YEAR"].count()
flights_mar = march.groupby("DAY")["YEAR"].count()
...

fig = go.Figure()


jan = fig.add_trace(go.Scatter(y=flights_jan, 
                            x=days, 
                            mode='lines'
                                           
                ))
feb = fig.add_trace(go.Scatter(y=flights_feb, 
                            x=days, 
                            mode='lines'
                                        
                ))
...


fig.show()


Solution 1:[1]

You should probably use a list or a dict instead :

flights = {"jan": january.groupby("DAY")["YEAR"].count(),
           "feb": february.groupby("DAY")["YEAR"].count()
           "mar": march.groupby("DAY")["YEAR"].count(),
           ...
           }

fig = go.Figure()
res = {}
for month, data in flights.items():
    res[month] = fig.add_trace(go.Scatter(y=data, 
                            x=days, 
                            mode='lines'                     
                 ))
#Here you can access the jan/feb/... variables by using res["jan"]/res["feb"]/...
fig.show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xiidref