According to the documentation, offline mode with plot.ly in Jupyter is supposed to work after calling:
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
Now I am trying to show the following plot:
trace0 = plotly.graph_objs.Scatter(
x=[1, 2, 3, 4],
y=[10, 15, 13, 17]
)
trace1 = plotly.graph_objs.Scatter(
x=[1, 2, 3, 4],
y=[16, 5, 11, 9]
)
iplot([trace0, trace1])
The result is much empty space in the cell output area.
Why is this not working with Jupyter Lab?
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.
In general, there are five different approaches you can take in order to display plotly figures: Using the renderers framework in the context of a script or notebook (the main topic of this page) Using Dash in a web app context. Using a FigureWidget rather than a Figure in an ipywidgets context.
The %matplotlib magic command sets up your Jupyter Notebook for displaying plots with Matplotlib. The standard Matplotlib graphics backend is used by default, and your plots will be displayed in a separate window.
If you are encountering problems using plotly with Dash please first ensure that you have upgraded dash to the latest version, which will automatically upgrade dash-core-components to the latest version, ensuring that Dash is using an up-to-date version of the Plotly. js rendering engine for plotly .
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
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()
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
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With