You will want to use graph_objects
instead of Plotly express, add your data as traces using go.Scatter
and the stackgroup
parameter to create overlapping area plots, and create a secondary y-axis. You can specify the secondary y-axis for your temperature data. I'll create a similar DataFrame as yours to illustrate my point.
import numpy as np
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
np.random.seed(42)
storm_data = np.repeat(3.5*10**9,21) + 10**8*np.random.normal(0,1,21)
flood_data = np.repeat(2*10**9,21) + 10**8*np.random.normal(0,1,21)
drought_data = np.repeat(1.4*10**9,21) + 10**8*np.random.normal(0,1,21)
df = pd.DataFrame({
'Decade':np.linspace(1900,2100,21),
'Storms':storm_data,
'Floods':flood_data,
'Droughts':drought_data,
'Temperature':np.random.normal(30,10,21)
})
## create a secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])
## add your data using traces
fig.add_trace(go.Scatter(
x=df.Decade,
y=df.Storms,
name='Storms',
stackgroup='one'
))
fig.add_trace(go.Scatter(
x=df.Decade,
y=df.Floods,
name='Floods',
stackgroup='one'
))
fig.add_trace(go.Scatter(
x=df.Decade,
y=df.Droughts,
name='Droughts',
stackgroup='one'
))
## specify the secondary y_axis for temperature
fig.add_trace(go.Scatter(
x=df.Decade,
y=df.Temperature,
name='Temperature',
),
secondary_y=True
)
fig.show()