I created a plotly plot using the following sample code with the aim to display different lines on different y axes. My problem ist, that as soon as I use the slider to change the data displayed the secondary y axis disappears and doesnโt come back even if I select the timeframe selected with the slider originally:
Problem occurs in Jupyter Notebook
Any clue how I can fix this?
Demo code to reproduce the issue:
import pandas as pd
import plotly.express as px
import datetime
df = pd.DataFrame(columns=['variable', 'value', 'time', 'date'],
data=[['A', 12, datetime.time(12, 0, 0), '2002-01-01'],
['A', 13, datetime.time(13, 0, 0), '2002-01-01'],
['A', 14, datetime.time(14, 0, 0), '2002-01-01'],
['A', 11, datetime.time(12, 0, 0), '2002-01-02'],
['A', 14, datetime.time(13, 0, 0), '2002-01-02'],
['A', 12, datetime.time(14, 0, 0), '2002-01-02'],
['A', 11, datetime.time(12, 0, 0), '2002-01-03'],
['A', 11, datetime.time(13, 0, 0), '2002-01-03'],
['A', 11, datetime.time(14, 0, 0), '2002-01-03'],
['B', 0.1, datetime.time(12, 0, 0), '2002-01-01'],
['B', 0.1, datetime.time(13, 0, 0), '2002-01-01'],
['B', 0.04, datetime.time(14, 0, 0), '2002-01-01'],
['B', 0.11, datetime.time(12, 0, 0), '2002-01-02'],
['B', 0.04, datetime.time(13, 0, 0), '2002-01-02'],
['B', 0.02, datetime.time(14, 0, 0), '2002-01-02'],
['B', 0.1, datetime.time(12, 0, 0), '2002-01-03'],
['B', 0.1, datetime.time(13, 0, 0), '2002-01-03'],
['B', 0.11, datetime.time(14, 0, 0), '2002-01-03']])
fig = px.line(data_frame=df, x='time', y='value',
color='variable', animation_frame='date')
fig.update_traces(line={'color':'blue'}, selector={'legendgroup': 'A'})
fig.update_traces(yaxis='y2', line={'color':'red'}, selector={'legendgroup': 'B'})
fig.update_layout(legend_orientation="h", legend=dict(x=0, y=1.1))
fig.update_layout(
yaxis=dict(
title="A axis",
titlefont=dict(
color="blue"
),
tickfont=dict(
color="blue"
)
),
yaxis2=dict(
title="B axis",
titlefont=dict(
color="red"
),
tickfont=dict(
color="red"
),
anchor="free",
overlaying="y",
side="right",
position=1
)
)