Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot.ly offline mode in jupyter lab not displaying plots

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.

enter image description here

Why is this not working with Jupyter Lab?

like image 867
clstaudt Avatar asked Jan 06 '19 17:01

clstaudt


People also ask

Why are plotlines not showing in JupyterLab?

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.

How do you display plotly plots?

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.

Which is used to display plots on the Jupyter Notebook?

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.

Why plotly Express is not working?

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 .


4 Answers

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

like image 144
Lucas Durand Avatar answered Oct 24 '22 04:10

Lucas Durand


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()
like image 24
Umut Kaya Avatar answered Oct 24 '22 03:10

Umut Kaya


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

like image 13
Makiavel Avatar answered Oct 24 '22 03:10

Makiavel


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())
like image 6
Dipanwita Mallick Avatar answered Oct 24 '22 03:10

Dipanwita Mallick