So I have been trying to get a hang of plotly and the x-axis range slider works until I use a workaround to create a graphic with a secondary y-axis via “make_subplots”.
However now the data that was attached to the secondary y-axis sticks in the range-slider preview. I cannot find a way to force it to be redrawn/refresh, see image attached. Once I refresh my browser it is fixed too, until I go to “both” again.
The code below yields this behavior. Is this itended and did I forget to clean something up?
For reproducibility I can share the data I used if needed.
from dash import Dash, dcc, html, Input, Output, callback
import plotly.express as px
import pandas as pd
from plotly.subplots import make_subplots
df = pd.read_csv(r"D:\t.txt",parse_dates=["Date"])
header = df.columns.values
date_column_name = header[0]
temp_column_name = header[1]
humd_column_name = header[2]
def Populate_Layout(application_handle):
application_handle.layout = html.Div([
html.H3(children='Monitoring', style={'textAlign': 'center'}),
html.Div([
html.Div([
dcc.RadioItems(
['Temperature', 'rel. Humidity', "Both"],
'Temperature',
id='crossfilter-data-shown',
labelStyle={'display': 'inline-block', 'marginTop': '5px'}
)
], style={'width': '49%', 'float': 'right', 'display': 'inline-block'})
], style={
'padding': '10px 5px'
}),
html.Div([
dcc.Graph(id='value_over_time_plot')
],style={'width': '50%', 'display': 'inline-block', 'padding': '0 20'}),
html.Div([
dcc.Graph(id='value_over_time_plot1')
], style={'width': '50%', 'display': 'inline-block', 'padding': '0 20'})
])
@callback(
Output('value_over_time_plot', 'figure'),
Input('crossfilter-data-shown', 'value'),
Input('value_over_time_plot', 'figure'),
)
def update_graph(date_type,lineplot=None):#,start_date,end_date):
start_date = df[date_column_name].min()
end_date = df[date_column_name].max()
dff = df[(df[date_column_name]>start_date) & (df[date_column_name]<end_date)]
if lineplot is not None:
lineplot["data"] = []
if date_type == "Temperature":
lineplot = px.line(dff, x=date_column_name, y=temp_column_name,render_mode='webg1')
lineplot["data"][0]["line"]["color"]="#ff0000"
lineplot.update_traces()
elif date_type == "Both":
lineplot = make_subplots(specs=[[{"secondary_y": True}]])
#lineplot.update_yaxes(title_text="<b>secondary</b> yaxis title", secondary_y=True)
plot_container = px.line(dff, x=date_column_name, y=humd_column_name,render_mode='webg1')
for trace in plot_container.select_traces():
lineplot.add_trace(trace,secondary_y=False)
plot_container = px.line(dff, x=date_column_name, y=temp_column_name,render_mode='webg1')
plot_container["data"][0]["line"]["color"]="#ff0000"
for trace in plot_container.select_traces():
lineplot.add_trace(trace,secondary_y=True)
else:
lineplot = px.line(dff, x=date_column_name, y=humd_column_name,render_mode='webg1')
lineplot.update_layout(xaxis_rangeslider_visible=True)
return (lineplot)
if __name__ == "__main__":
PORT = 8080
ADDRESS = "127.0.0.84"
app = Dash(__name__)
Populate_Layout(application_handle=app)
app.run(host=ADDRESS,port=PORT,debug=True)
app.show()