Plotly express plots automatically open new tabs when using plotly offline

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.

Hi @guy,

plotly.offline.plot saves plots to a file with the same name very time (unless you provide your own file name), 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.

You could try out the new 'browser' renderer. The renderers subsystem is a generalization of the plot/iplot approaches that we’ve built for plotly.py version 4 (documentation will be released alongside version 4), but it’s available for testing in version 3.8+ (I’d recommend 3.10). Here’s how that would work

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)

This renderer doens’t write out temporary files, instead it sets up a single use webserver and serves the plot to the browser over a local port so there shouldn’t be any risk of the wrong plot being displayed. You can also set pio.renderers.default to 'chrome', 'chromium', or 'firefox' if you want to be explicit about which browser is used.

If you give that a try, let us know how it goes!
-Jon

1 Like