94

I have been trying to solve this issue for hours. I followed the steps on the Plotly website and the chart still doesn't show in the notebook.

This is my code for the plot:

colorway = ['#f3cec9', '#e7a4b6', '#cd7eaf', '#a262a9', '#6f4d96', '#3d3b72', '#182844']

data = [
    go.Scatter(
        x = immigration.columns,
        y = immigration.loc[state],
                   name=state) for state in immigration.index]

layout = go.Layout(
    title='Immigration',
    yaxis=dict(title='Immigration %'),
    xaxis=dict(title='Years'),
    colorway=colorway,
    font=dict(family='Courier New, monospace', size=18, color='#7f7f7f')
)

fig = go.Figure(data=data, layout=layout)
iplot(fig)

And this is everything I have imported into my notebook:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot

init_notebook_mode(connected=True)  
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
Guillermina
  • 3,127
  • 3
  • 15
  • 24
  • You cannot create multiple `traces` in that way, for loop did not work in that case. If you have not so much `state`, try to create each `trace` for each `state`. Just as showed [here](https://plot.ly/python/line-and-scatter/#line-and-scatter-plots). And change each `y` to name of state. Then create list of traces and named it `data` as at example. – Dmitriy Kisil Oct 12 '18 at 09:52
  • @Oysiyl thanks, but that actually doesn't asnwer my question. The plot is showing, but in a separate tab. What I would like to do is to be able to see it in my notebook not in a separate tab. – Guillermina Oct 12 '18 at 16:09

12 Answers12

95

You need to change init_notebook_mode call and remove connected=True, if you want to work in offline mode.

Such that:

# Import the necessaries libraries
import plotly.offline as pyo
import plotly.graph_objs as go
# Set notebook mode to work in offline
pyo.init_notebook_mode()
# Create traces
trace0 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[10, 15, 13, 17]
)
trace1 = go.Scatter(
    x=[1, 2, 3, 4],
    y=[16, 5, 11, 9]
)
# Fill out data with our traces
data = [trace0, trace1]
# Plot it and save as basic-line.html
pyo.iplot(data, filename = 'basic-line')

Output should be shown in your jupyter notebook:

My example

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dmitriy Kisil
  • 2,858
  • 2
  • 16
  • 35
48

In case you want to use Jupyter lab, you will have to install the plotly jupyterlab extension:

https://plotly.com/python/getting-started/#jupyterlab-support-python-35

Simple solution: jupyter labextension install jupyterlab-plotly

Restart Jupyter Lab after installing the extension.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hammer. Wang
  • 686
  • 5
  • 12
30

To use a plotly version below 5.0 in Jupyter Lab make sure you have ipywidgets and plotly installed and then run the following:

jupyter labextension install jupyterlab-plotly

OPTIONAL: Jupyter widgets extension:

jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget

Source docs

And here's the troubleshooting guide for plotly with Jupyter Lab.

As of Plotly version 5.0, I am able to create a new conda environment with Python 3.9 and then pip install plotly jupyterlab, and run Jupyter Lab and render plots without any other package or extension installs.

jeffhale
  • 3,759
  • 7
  • 40
  • 56
  • 1
    You may remove the version part altogether to get the latest build by running `jupyter labextension install jupyterlab-plotly` – Ébe Isaac Jul 07 '20 at 12:05
  • installing the optional (having the 1st) crashed the visualization, I had to install the first one after uninstalling both. – Ferroao Jun 18 '21 at 03:45
  • 1
    This seems incorrect "As of version 5.0, you no longer need to install any extensions.". Not a savvy, but I installed 5.1 and had the issue until I installed the extension and restarted jupyter. – mlg Aug 12 '21 at 16:29
  • I just confirmed that I did not need to install any extensions and Plotly 5.2.1 rendered fine for me using JupyterLab 3.1.7 on a Mac. – jeffhale Aug 16 '21 at 23:26
21

Those having trouble (even after installing extension) may try changing renderer. It worked for me on JupyterLab on Chrome.

Note that this will create a iframe figure directory not pure html file.

If using jupyterlab install JupyterLab extension

jupyter labextension install jupyterlab-plotly

Add this line before py.iplot or fig.show()

import plotly.io as pio
pio.renderers.default = 'iframe'
Gorkem
  • 701
  • 8
  • 22
  • 2
    Adding `pio.renderers.default = 'iframe'` worked for me without needing to restart Jupyter Lab – Justas May 06 '22 at 13:12
  • I'd like to add that in order for it to work the order of commands was important. First use `init_notebook_mode()` and then call `pio.renderers.default = 'iframe'` – waykiki May 17 '23 at 08:05
  • `pio.renderers.default = 'iframe'` works for me, thank you! – Ezio Shiki Jul 12 '23 at 08:21
11

Being new to Plotly, I had the same issue. I tried all of the above things but still got blank graph. Turns out, only installing the jupyterlab extensions is enough, but you need to shutdown and restart the jupyterlab itself. Just restarting the kernel didn't help.

nayakasu
  • 889
  • 1
  • 11
  • 32
  • 1
    This worked for me (installing extension + restarting kernel fails + stopping and restarting jupyter). Thank you. Looks like this comment on the other answer might be incorrect: "As of version 5.0, you no longer need to install any extensions" – mlg Aug 12 '21 at 16:28
8

I’m running plotly=5.9.0 in jupyter-notebook=6.5.2 through my DataSpell IDE. Although currently functional as of 20 April, 2023, many of the historical solutions for rendering Plotly graphics in Jupyter Notebooks are deprecated according to this post -> (see my comments below).

The final solution that worked for me:

import plotly.graph_objs as go
import plotly.io as pio
pio.renderers.default = “plotly_mimetype+notebook”

fig = ...

fig.show()

(Had this not worked, the next alternative I would have tried is:

pio.renderers.default = "plotly_mimetype+notebook_connected”

An intermediate solution that worked, but that I wasn’t terribly excited about implementing, was to save the figure locally in an HTML file, then import it back into the notebook using:

# Save the figure locally
fig.write_html("my_visualisation.html")

# Show the figure
from IPython.display import HTML
HTML(filename="my_visualisation.html")

The initial recommendation from a Plotly maintainer in 2021 at the post I referenced above was to use:

import plotly.io as pio
pio.renderers.default = "notebook_connected"

fig = ...

fig.show()

and nothing more since the other solutions are now built-in, and because “our current development philosophy for this library: we are trying to funnel everything through fig.show and we've stripped out all mentions of plotly.offline from the docs because the online/offline distinction has historically really confused and scared folks :)”.

For example,

import plotly.offline as pyo
pyo.init_notebook_mode()

or

pyo.init_notebook_mode(connected=True)

are no longer required. However, the 2021 recommendation I quoted didn’t work for me, nor did another comment on the same page: pio.renderers.default = "notebook”.

user20885351
  • 83
  • 1
  • 4
7

Assuming you are using JupyterLab, accordingly to Plotly Troubleshooting

In order to use plotly in JupyterLab, you must have the extensions installed as detailed in the Getting Started guide. There are two extensions: jupyterlab-plotly for rendering figures with fig.show() and plotlywidget for the FigureWidget.

Assuming that you have installed all the libraries correctly (make sure you have ipywidgets and nodejs installed) and assuming one is using conda, access conda prompt for the environment one is working (the "Server" environment).

List the labs' extensions with

jupyter labextension list

In my case I got

JupyterLab v2.2.9
No installed extensions

Then I will need to install the extensions jupyterlab-plotly (the library nodejs will be required now)

jupyter labextension install jupyterlab-plotly@4.14.3

and plotlywidget [optional]

jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4.14.1

Now you'll be able to visualize your plots.


Note

If you use JupyterLab with multiple python environments, the extensions must be installed in the "server" environment, and the plotly python library must be installed in each "processing" environment that you intend to use.

Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
6

In case you are facing this issue on jupyter notebook instead of jupyter lab, you may try the following command

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

This worked for me without restarting

MNA
  • 183
  • 2
  • 8
5

If the other answers do not work for you then check if WebGL is enabled for your browser. You can check and enable WebGL in Chrome by following the below steps.

  1. In the address bar, type chrome://flags/ and press ENTER
  2. Enable all the options with WebGL keyword
  3. Click Relaunch Now. Google Chrome will restart and your new settings will be applied without closing other tabs
Manas
  • 888
  • 10
  • 20
3

I was getting the same error, and tried all suggestions, but I've got "deprecated" error messages and it didn't work.

It stated that I could be able to install the packages from my package manager conda. So the following commands worked fine for me:

conda install nodejs

and then

conda install jupyterlab-plotly

I also had to restart JupyterLab.

Regards,

0

Changing default web browser to Chrome solved the issue in my case. I repeated this instructions, though I had Google Chrome as a default browser already. I cite them here with slight modification:

step1: Go to search menu of windows and type "default apps".

step 2: go to WEB BROWSER title and choose Google Chrome once again.

step3: Launch jupyter notebook, it will be opened in in Google Chrome

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33131848) – Dom Nov 12 '22 at 19:29
0

I've been using this simple config in Jupyter/JupyterLab:

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

It has been functioning properly for me. The only inconvenience is that it might take a few seconds in case of heavy visualizations.

Reza Keshavarz
  • 688
  • 2
  • 9
  • 17