plot.ly offline mode in jupyter lab not displaying plots
A couple things might be happening. For some reason is your Notebook "Not Trusted?" This will stop the Plotly javascript from rendering anything below.
Another possibility is that you don't have the Plotly extension installed for Jupyter Lab. In Lab, as compared to Notebooks, there are a lot more restrictions on what javascript can execute, so packages need to have extensions installed in order to display anything beneath the cell.
Look for the plotly-extension with
jupyter labextension list
and install it if missing with: jupyter labextension install jupyterlab-plotly
Plotly-extension has been deprecated, use another:
jupyter labextension install jupyterlab-plotly
it worked for me and it has no compatibility issues with the latest versions of plotly (currently 4.9.0) / jupyterlab
source: https://plotly.com/python/getting-started/ , https://www.npmjs.com/package/@jupyterlab/plotly-extension
Try changing the renderer:
import plotly.io as pio
pio.renderers.default = 'iframe' # or 'notebook' or 'colab' or 'jupyterlab'
You can loop through the list like this to see what fits you
import pandas as pd
import plotly.express as px
import plotly.io as pio
for text in pio.renderers:
print(text)
pio.renderers.default = text
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
One way to do it will be using HTML to help JupyterLab show figures. One example will be:
import plotly.express as px
from IPython.display import HTML
df = px.data.tips()
fig = px.scatter(df, x='total_bill', y='tip', opacity=0.65,
trendline='ols', trendline_color_override='darkblue')
HTML(fig.to_html())