mercury
mercury copied to clipboard
Plotly figure is not updating, when changing mr.Select() widget in slide show
My notebook contains a plotly graph. It appears in mercury at the first launch, but when I change one of the widgets (choose another value from the drop down list), the graph just disappears. In the meantime, everything works well, when I plot via static matplotlib graph. Do I need to use some kind of construction, like shown on the website using _ = plt.plot()?
Hmm, could you please share a code snippet so I can reproduce the issue locally.
Cannot upload the notebook, so I am going to share a minimum reproducible example with few citations below:
import mercury as mr
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import plotly.express as px
app = mr.App(title='Some app',
description='Some discr')
# load some data
df = pd.DataFrame(columns=['prod_name', 'vals'])
df.prod_name = ['prod1']*10 + ['prod2']*10
df.vals = np.random.rand(20)
prod = mr.Select(label='Select product', value='prod1', choices=df.prod_name.unique())
df_sub = df[df.prod_name==prod.value]
fig = px.line(df_sub,
x=df_sub.index,
y='vals')
fig.update_layout(
hovermode = False,
showlegend = True,
legend = {'title':''},
template = 'plotly_white',
paper_bgcolor = '#f5f5f2',
plot_bgcolor = '#f5f5f2')
fig.show()
Hi @max-poltora,
Thank you for code example for reproducing the issue. Please try different way to display Plotly chart in the notebook.
# new imports
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# code to make a plot
df_sub = df[df.prod_name==prod.value]
fig = go.Scatter(x=df_sub.index, y=df_sub.vals)
pyo.iplot([fig])
Hi, @pplonski,
Unfortunately, this approach also does not work for me... The result is just the same: plot disappears, when widget is updated.
Below is plotly demo that is using plotly.express. Is it working for you?
import plotly.express as px
import mercury as mr
a = mr.Slider(label="a value", value=1)
b = mr.Slider(label="b value", value=1)
c = mr.Slider(label="c value", value=1)
fig = px.bar(x=["a", "b", "c"], y=[a.value, b.value, c.value])
fig.show()
Notebook
Web App:
Morning, @pplonski,
Yes, the above example works. But I added app = mr.App() in the second cell.
Hi @max-poltora,
Is it working for you with mr.App? I've checked locally, and for me it works.
Yes, it does work this way for me too.
@pplonski, sorry, the example with mr.Slider works, but the problem with mr.Select and plotly is still there...
Hmm, I will need an example to reproduce your problem. I was trying with my example code and it is working with Select widget:
- Have you tried on different web browser?
- Is your code working without
mercurywidgets?
I am trying the following code (modified from the one sent earlier to this chat):
import mercury as mr
import pandas as pd
import numpy as np
import plotly.offline as pyo
import plotly.graph_objs as go
# load some data
df = pd.DataFrame(columns=['prod_name', 'vals'])
df.prod_name = ['prod1']*10 + ['prod2']*10
df.vals = np.random.rand(20)
app = mr.App()
prod = mr.Select(label='Select product', value='prod1', choices=df.prod_name.unique())
df_sub = df[df.prod_name==prod.value]
fig = go.Scatter(name=prod.value, x=df_sub.index, y=df_sub.vals)
layout = go.Layout(
xaxis = {'title': 'Month', 'showspikes': False, 'spikethickness':1, 'dtick':1},
yaxis = {'title': 'Normalized price value', 'showspikes': True, 'spikethickness':1},
hovermode = False,
showlegend = True,
legend = {'title':''},
template = 'plotly_white')
pyo.iplot({'data': fig, 'layout': layout})
The result is on the gif below.
I tried Google Chrome and Mozilla Firefox, cleared cache, restarted anaconda prompt and notebook - result is the same.
The code is working with and without widgets on my computer.
Your code is working fine:
I was running notebook as web app, not as presentation. I see from your screenshot, that you are serving notebook as presentation. Do you have only one slide with plot?
When running in presentation mode, then I have problems with Plotly as well. Hard to say where is a problem ...
Could you please try other plotting package?
Thank you for your time and efforts! Running as web app works for me too. The plt.plot() command works also fine in presentation mode, updates with changing drop-down widget.