Add a legend on a mutliple axis plot

Hello all.

For a monitoring interface, I’m plotting time series which I store in a DataFrame that look like this :

                     temp        hum        press  windspeed     winddir
date                                                                    
2025-04-01 13:20:01 -4.25  62.500000  1004.587054       2.66  277.320007
2025-04-01 13:15:01 -4.27  61.000000  1004.580975       3.76  241.729996
2025-04-01 13:10:01 -4.34  60.799999  1004.562020       3.86  242.029999
2025-04-01 13:05:01 -4.32  59.700001  1004.587054       4.76  235.229996
2025-04-01 13:00:01 -4.25  58.799999  1004.588962       1.18  206.639999
2025-04-01 05:25:01 -7.72  71.699997  1007.751942       2.55  172.240005
2025-04-01 05:20:01 -7.88  71.000000  1007.825971       2.54  182.600006
2025-04-01 05:15:01 -8.02  70.199997  1007.812023       1.96  180.440002
2025-04-01 05:10:01 -8.06  69.400002  1007.848978       1.65  169.160004
2025-04-01 05:05:01 -8.10  68.599998  1007.926941       2.13  215.539993

I’m creating 4 different graphs from this using the following code :

import plotly.express as px
from plotly.subplots import make_subplots


temp_graph = px.line(data, y='temp')
press_graph = px.line(data, y='press')
hum_graph = px.line(data, y='hum')
wd_spd = px.line(data, y='windspeed')
wd_dir = px.line(data, y='winddir')
wd_dir['data'][0]['line']['color'] = 'red'
wd_dir.update_traces(yaxis='y2')
wind_graph = make_subplots(specs=[[{'secondary_y': True}]])
wind_graph.add_trace(wd_spd.data[0])
wind_graph.add_trace(wd_dir.data[0])

The layout of my app is then updated with this new graphs (if needed, I can put together a ‘functionning app’ for testing)

My issue is that I can’t manage to add a legend on the ‘wind_graph’ that would basically say :
trace 1 : Wind Speed
trace 2 : Wind Direction

I’ve tried several things, and I’m not even sure that the multiple axis graph I’m creating is the “correct” way to do this

Hey @Aprots.

Iḿ not sure if I understand this correctly. But why don’t you just add the two traces directly to the figure?

fig = px.line(data, y=[windspeed, winddir])

I guess data is a pandas.DataFrame()

Hello @AIMPED,

I want to have two y axis, one on the right and one on the left because the data are on different scales. Windspeed is between 0-20 (usually) and winddir will be between 0-360. So if they are on the same Y axis, one of them will just be a straight line basically. I wanted to have them on the same graph because they are related, but maybe it will be easier if I find another way to represent the wind direction.

And yes, data is a pandas.DataFrame(), sorry for not making it clear.