Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly gives an empty field as output in jupyter lab

Tags:

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?

like image 760
Leda Grasiele Avatar asked Mar 01 '19 00:03

Leda Grasiele


People also ask

Can plotly be used in Jupyter notebook?

Simply visit plot.ly and select the + Create button in the upper right hand corner. Select Notebook and upload your Jupyter notebook (. ipynb) file! The notebooks that you upload will be stored in your Plotly organize folder and hosted at a unique link to make sharing quick and easy.

How many data points can plotly handle?

Due to browser limitations, the Plotly SVG drawing functions have a hard time graphing more than 500k data points for line charts, or 40k points for other types of charts. Here are some suggestions: (1) Use the `plotly. graph_objs. Scattergl` trace object to generate a WebGl graph.

Why is plotly offline?

Setting for Offline Plotting Plotly allows you to generate graphs offline and save them in local machine. The plotly. offline. plot() function creates a standalone HTML that is saved locally and opened inside your web browser.

Which is the correct syntax to install a package plotly?

To install the package, open up terminal and type $ pip install plotly or $ sudo pip install plotly . Plotly's graphs are hosted using an online web service, so you'll first have to setup a free account online to store your plots.


2 Answers

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.

like image 193
YaOzI Avatar answered Sep 29 '22 08:09

YaOzI


@YaOzI answer is partially correct but @jupyterlab/plotly-extension is deprecated and not supported by the official plotly team as you can read here.

This can give you the following error:

ValueError: The extension "@jupyterlab/plotly-extension" does not yet support the current version of JupyterLab.


Conflicting Dependencies:
JupyterLab                        Extension      Package
>=2.2.1 <2.3.0                    >=1.3.0 <2.0.0 @jupyterlab/rendermime-interfaces
See the log file for details:  /tmp/jupyterlab-debug-a3i3t9j4.log

>>> TL;DR:

Following the official advice, this is what worked for me (make sure your kernel is shut down, otherwise you will need to run jupyter lab build):

jupyter labextension install jupyterlab-plotly

And (just to be sure it worked):

jupyter labextension list

May give you something like this:

JupyterLab v2.2.9
Known labextensions:
   app dir: /home/user/anaconda3/envs/your-py-env/share/jupyter/lab
        jupyterlab-dash v0.3.0  enabled  OK
        jupyterlab-plotly v4.14.1  enabled  OK

like image 25
David Avatar answered Sep 29 '22 09:09

David