plotly.offline.iplot gives a large blank field as its output in Jupyter Notebook/Lab
Asked Answered
K

16

37

I am trying to create a Sankey chart in a Jupyter notebook, basing my code on the first example shown here.

I ended up with this, which I can run without getting any errors:

import numpy as npy
import pandas as pd
import plotly as ply

ply.offline.init_notebook_mode(connected=True)

df = pd.read_csv('C:\\Users\\a245401\\Desktop\\Test.csv',sep=';')

print(df.head())
print(ply.__version__)

data_trace = dict(
    type='sankey',
    domain = dict(
      x =  [0,1],
      y =  [0,1]
    ),
    orientation = "h",
    valueformat = ".0f",
    node = dict(
      pad = 10,
      thickness = 30,
      line = dict(
        color = "black",
        width = 0.5
      ),
      label =  df['Node, Label'].dropna(axis=0, how='any'),
      color = df['Color']
    ),
    link = dict(
      source = df['Source'].dropna(axis=0, how='any'),
      target = df['Target'].dropna(axis=0, how='any'),
      value = df['Value'].dropna(axis=0, how='any'),
  )
)
print(data_trace)

layout =  dict(
    title = "Test",
    height = 772,
    width = 950,
    font = dict(
      size = 10
    ),    
)
print(layout)

fig = dict(data=[data_trace], layout=layout)
ply.offline.iplot(fig, filename='Test')

With the csv-file looking like this:

Source;Target;Value;Color;Node, Label
0;2;2958.5;#262C46;Test 1
0;2;236.7;#262C46;Test 2
0;2;1033.4;#262C46;Test 3
0;2;58.8;#262C46;Test 4
0;2;5.2;#262C46;Test 5
0;2;9.4;#262C46;Test 6
0;2;3.4;#262C46;Test 7

It seems to run fine, with the various outputs looking right at a first glance, but the final output from ply.offline.iplot(fig, filename='Test') just shows a large blank field: enter image description here The terminal looks like this after having run all the cells in the notebook once: enter image description here

Can someone please point me to where I am going wrong here?

Katzir answered 1/2, 2018 at 10:10 Comment(10)
i think you're using notebook. Maybe you need %matplotlib inline like this but for plotly packageKnighterrant
Redirect output to file but Where is the Proceder ? If you create an I/O entity and receive output, you must make the necessary routing and addressing.Wisniewski
Can you check the terminal where you started the notebook for any error messages or warnings?Cantina
@elphz I have added a screenshot to the question of what the terminal looks like after having run all cells in the notebook once.Katzir
Hmm, not too useful. Your code produces an image for me, but I have had similar issues with plotly offline in Jupyter in the past - sometimes it's surprisingly inconsistent when/why the plots fail to appear. It may be worth a try starting with an increased data rate limit - jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10Cantina
@elphz Thanks. I may just try to do the same thing on my private computer, to avoid stupid corporate firewalls, so I can see if it works when not using plotly.offlineKatzir
@elphz Increasing the data rate limit seems to have fixed it - if you want to put this into an answer, I'll award you the bounty.Katzir
great - glad that fixed it! I was surprised it didn't give an error warning with that issue. just added it as an answer.Cantina
It turned out the "offline" api can not really be offline. you still need internet connection to load plotly.js from its cdn. otherwise it give you a blank window.Mettle
I'm from the future using JupyterLab 4 and it seems plotly isn't compatible with that version according to: community.plotly.com/t/does-plotly-work-with-jupyterlab-4/76095Aspasia
C
28

I have had similar issues with plotly offline in Jupyter in the past - sometimes it's surprisingly inconsistent when/why the plots fail to appear. It may be worth a try starting with an increased data rate limit.

jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10

Cantina answered 9/2, 2018 at 16:12 Comment(2)
Was seeing this issue in jupyter lab. When i switched to the notebook app, this suggesting did the trick for me. Is there a way to set this parameter through jupyter lab?Fsh
@MichaelSzczepaniak Did you find a way through this for jupyter lab?Amyl
E
19

I tried all the solutions suggested here, but none of them worked for me. What solved the issue was adding:

import plotly.io as pio
pio.renderers.default='notebook'

and also using fig.show("notebook") rather than simply fig.show(), as suggested here.

Emersen answered 6/7, 2020 at 8:53 Comment(0)
M
12

For me helped change notebook to Trusted (this enabled Javascript and give plotly way to build graph into the jupyter notebook.).

This option you can found here:

In upper right side of notebook

Marbleize answered 26/12, 2018 at 10:5 Comment(1)
Thank you, this worked. Though I wonder why because it worked before on the same notebook when it was "untrusted".Serieswound
T
3

When using Google Colab, include the snippet -

import plotly.io as pio
pio.renderers.default = 'colab'

Or use the overall import statements as -

import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.io as pio
pio.renderers.default = 'colab'
from plotly.offline import init_notebook_mode, iplot

This will set the rendering to Colab style and the plots will be displayed.

Hope this helps.

Theocrasy answered 13/3, 2020 at 8:12 Comment(0)
N
2

I can get the correct display with jupyter notebook server (without any additional options), but get a blank block with jupyter lab server. Related version info:

$ jupyter lab --version
0.35.5
$ jupyter notebook --version
5.7.8
$ python -c "import plotly; print(plotly.__version__)"
3.10.0

So for those who are using JupyterLab, to properly display the offline plotly graphs in JupyterLab, we need to install the plotly-extension with following commands (following is excerpted from a related answer):

$ jupyter labextension install @jupyterlab/plotly-extension
$ jupyter labextension list 
$ jupyter lab build
Neona answered 26/6, 2019 at 16:54 Comment(1)
For one thing: Installing extensions with the jupyter labextension install command is now deprecated and will be removed in a future major version of JupyterLab. Also this seems impossible if JupyterLab is recent as there is a package dependence conflict on rendermime-interfacesAspasia
S
1

If none of these answers worked for you. Try this one

just do this in conda prompt opened with administrator permission

pip install pip --upgrade
conda upgrade notebook or pip install notebook --upgrade

conda install -c conda-forge ipywidgets or pip install ipywidgets

You need to have the latest versions of jupyter notebook and ipywidgets.

Surfeit answered 20/6, 2019 at 9:35 Comment(0)
H
1

I have myself encountered a similar issue, with a sunburst plot. The blank plot was due to invalid data. I would have expected to get an error at runtime, but it appears that in some rare cases, no error is raised, and a blank plot is displayed instead.

Therefore, check your data validity, or perform a test with dummy data provided on plotly doc, in order to test if the problem comes from data or plotly/notebook interface.

Hyperkinesia answered 30/10, 2020 at 6:54 Comment(1)
Encountered at also in go.Figure(data=[go.Pie()]), rechecked the data and you are right.Aga
W
1

My response might be a little too late, but along with Plotly its also important to import plotly.graph.

These are the packages you need to import. I was getting the blank outputs too, and this worked for me.

from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
Westfall answered 15/2, 2022 at 6:13 Comment(0)
F
1

I figured out that the environment (conda, pipenv, ...) in which Jupyter Lab runs and the environment of the kernel which is supposed to run Plotly both require slightly different dependencies on their own.
For me the following works with jupyterlab==3.5.3 and plotly==5.14.1.

Environment for Jupyter Lab:
jupyterlab==3.5.3 ipywidgets jupyter-dash nbformat

Kernel environment with Plotly:
plotly==5.14.1 ipywidgets jupyter-dash nbformat

Additionally, run jupyter lab build after installation of all dependencies because jupyter-dash requires it.

The environment of Jupyter Lab doesn't seem to need plotly. Note that these lists of dependencies are what I can confirm to work but it might be possible to omit few of these dependencies. Or it might be case that there are transitive dependencies not listed here which are the ones which are actually required - I don't know really.

Frisk answered 19/4, 2023 at 8:30 Comment(0)
G
1

What worked for me was to disable the Ad Blocker in my browser.

I was using AdBlock on Chrome Browser and disabling it for http://localhost:8888/ enabled me to see the plotly charts within the Jupyter notebook.

Gertrudgertruda answered 14/1, 2024 at 3:45 Comment(0)
T
0

From the quickstart in the README

This displays a figure, while none of the other answers here worked for me:

import plotly.graph_objs as go
fig = go.FigureWidget()
# Display an empty figure
fig

This (in a new cell) modifies the above plot with a bar and line chart:

# Add a scatter chart
fig.add_scatter(y=[2, 1, 4, 3])
# Add a bar chart
fig.add_bar(y=[1, 4, 3, 2])
# Add a title
fig.layout.title = 'Hello FigureWidget'

If that doesn't work, make sure your install is good, e.g. pip install --user --upgrade plotly if you installed with pip

Temple answered 6/3, 2019 at 14:11 Comment(1)
Hi, Job. Do you have another tip? I am using Colab and I'm trying to plot a go.Sankey(). I tried you tip, but did not work for me.Speculator
R
0

Some of above methods did work for me. But I solve this kind of problem untimately according to this discussion and the official troubleshooting guide.

  • Step 1: Close your jupyter lab. Then, uninstall plotly and some extensions.
$ conda remove plotly
$ jupyter labextension uninstall jupyterlab-plotly
$ jupyter labextension uninstall plotlywidget
  • Step2: Open terminal from the same environment as JupyterLab was launched to rebuild jupyter lab.
$ jupyter lab build
  • Step 3: Reinstall plotly and jupyter lab extensions with same versions
$ conda install plotly
$ jupyter labextension install jupyterlab-plotly
$ jupyter labextension install plotlywidget

Above precedures work fine for me in jupyter lab. I think we can try same precedures like it in jupyter notebook.

Relentless answered 21/6, 2021 at 7:10 Comment(1)
I'm having this issue where inline plotly plots open in jupyter notebook but not jupyter lab. How do I verify that the terminal is in the same environment that jupyterlab was luanched from?Quintain
A
0

On my side the culprit was the "Darker Jupyter" addon I was using on firefox.

Anuria answered 28/7, 2021 at 8:55 Comment(0)
O
0

include these lines:

from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)

works for me

Overstride answered 31/8, 2021 at 14:20 Comment(0)
L
0

I had almost a similar issue while trying to plot a heatmap in colab, for me the below solution helped:

import plotly.graph_objs as go
from plotly.offline import iplot
import chart_studio.plotly as py

# these two lines are what allow your code to show up in a notebook
import plotly.io as pio
pio.renderers.default='colab'

corr = data.corr()

trace = go.Heatmap(z=corr.values,
                  x=corr.index.values,
                  y=corr.columns.values)
data=[trace]
iplot(data, filename='basic-heatmap')
Lour answered 22/2, 2022 at 9:24 Comment(0)
A
0

sometimes its just the version of Jupyter Notebook / Plotly needing to be downgraded

Areta answered 25/7, 2024 at 15:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.