plotly express plots automatically open new tabs when using plotly offline

Viewed 5569

When plotting with plotly-express (px) and plotly.offline, every once in a while (maybe once an hour) all previous plots I had re-open on my browser.

I tried accessing the ExpressFigure object to see if the problem is there - but couldn't manage.

import plotly_express as px
from plotly.offline import plot 
iris = px.data.iris()
scatter_plot = px.scatter(iris, x="sepal_width", y="sepal_length")
plot(scatter_plot)

This will plot the data in a new tab (saving an html file in the local directory), that will reopen every once in a while, replotting the instance.

(This required plotly_express and pandas installed. to install px, simply run pip install plotly_express).

2 Answers

Answer was provided by plotly team, and I'm sharing here. plotly.offline.plot saves plots to a file with the same name very time so there might be an occasional issue where the browser is being opened before the file is fully written to disk, thereby displaying the previous figure.

The following method solves the problem using plotly.io and plots a single figure at a time:

import plotly_express as px
import plotly.io as pio
pio.renderers.default = 'browser'
iris = px.data.iris()
scatter_plot = px.scatter(iris, x="sepal_width", y="sepal_length")
pio.show(scatter_plot)

To display plots inline you can use plotly.offline.iplot instead of plotly.offline.plot but this is actually what ExpressFigure does under the hood so it's not clear why you'd need to do it explicitly.

Related