Search code examples
pythonjupyter-notebookplotlyjupyterjupyter-lab

Plotly gives an empty field as output in jupyter lab


I'm using plotly at jupyter lab, but I'm getting a blanked output. I'm having exactly the same problem described here: plotly.offline.iplot gives a large blank field as its output - why?

And I tried what they suggested in the answers, but it didn't work.

Here is the code I'm using:

import pandas as pd
import numpy as np
%matplotlib inline

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
cf.go_offline()

df = pd.DataFrame(np.random.randn(100,4), columns='A B C D'.split())
df2 = pd.DataFrame({'category':['A','B','C'], 'values':[32,43,50]})

df.iplot(kind='scatter', x='A',y='B', mode='markers', size=10)

One of the suggestions was to change the notebook to 'trusted'. Do you know how can I do it in jupyter lab?


Solution

  • To properly display the plotly offline graphs in JupyterLab,

    Step 1: We need to first install the plotly-extension for JupyterLab:

    $ jupyter labextension install @jupyterlab/plotly-extension
    

    (Note that the above step requires Node.js >= 4, if Node.js is not available on your OS, install it from its Official Website.)

    Step 2: Check the status after the installation of @jupyterlab/plotly-extension:

    $ jupyter labextension list 
    JupyterLab v0.35.5
    Known labextensions:
       app dir: /Users/yourname/anaconda3/share/jupyter/lab
            @jupyterlab/plotly-extension v0.18.2  enabled  OK
    
    Build recommended, please run `jupyter lab build`:
        @jupyterlab/plotly-extension needs to be included in build
    

    Step 3: Follow the suggestion, re-build the JupyterLab with its newly installed labextensions:

    $ jupyter lab build
    

    After these, restart JupyterLab, and set plotly.offline.init_notebook_mode(connected=True) at the start of each notebook session, then plotly.offline.iplot should correctly display the plots in the notebook.