Plotly chart not showing in Jupyter notebook
Question:
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)
Answers:
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:
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.
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
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.
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 [email protected]
and plotlywidget
[optional]
jupyter labextension install @jupyter-widgets/jupyterlab-manager [email protected]
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.
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.
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'
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.
- In the address bar, type chrome://flags/ and press ENTER
- Enable all the options with WebGL keyword
- Click Relaunch Now. Google Chrome will restart and your new settings will be applied without closing other tabs
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
Changing default web browser to Chrome solved the issue in my case. I was using this instructions.
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)
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:
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.
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
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.
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 withfig.show()
andplotlywidget
for theFigureWidget
.
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 [email protected]
and plotlywidget
[optional]
jupyter labextension install @jupyter-widgets/jupyterlab-manager [email protected]
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.
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.
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'
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.
- In the address bar, type chrome://flags/ and press ENTER
- Enable all the options with WebGL keyword
- Click Relaunch Now. Google Chrome will restart and your new settings will be applied without closing other tabs
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
Changing default web browser to Chrome solved the issue in my case. I was using this instructions.