Python Plotly : How to conditionally fill an area plot ranged by non zero horizontally lines?

51 Views Asked by At

Here is my result with seaborn :

El nino event

With plotly I have :

# Create a simple line chart with Plotly Graph Objects
fig = go.Figure()

# Add a line trace
fig.add_trace(go.Scatter(x=df_oce_nino["Date"], y=df_oce_nino["Value"], line=dict(color="black")))

# create 2 columns to fill
mask_nino = df_oce_nino["Value"] >= 0.5
mask_nina = df_oce_nino["Value"] <= -0.5
df_oce_nino['Value_nino'] = np.where(mask_nino, df_oce_nino["Value"], 0.5)
df_oce_nino['Value_nina'] = np.where(mask_nina, df_oce_nino["Value"], -0.5)

# Add the filled area between the curve and 0.5
fig.add_trace(go.Scatter(
    x=df_oce_nino["Date"],  
    y=df_oce_nino["Value_nina"],
    fill='toself',
    fillcolor='steelblue',
    line=dict(color="rgba(255,255,255,0)"),  # Make the line color transparent
    name="La Niña",
    mode='none'),
)

# Add the filled area between the curve and -0.5
fig.add_trace(go.Scatter(
    x=df_oce_nino["Date"] ,
    y=df_oce_nino["Value_nino"],
    fill='toself',
    fillcolor='brown',
    line=dict(color="rgba(255,255,255,0)"),  # Make the line color transparent
    name="El Niño",
    mode='none'),
)

# Update layout
fig.update_layout(title="NOAA Oceanic Nino for the 2011-2020 decade", xaxis_title="Date", yaxis_title="Index value (°C)")

# Show the figure
fig.show()

plotly graph

Does somebody has an idea how to fill it correctly because I can't find it here : https://plotly.com/python/filled-area-plots/

0

There are 0 best solutions below