My goal is to display plotly charts in GitHub (interactive or not). The reason that I switch to Plotly instead of using matplotlib.pyplot or pandas.Series.plot is that it offers you interactive charts.
However, after pushing the .ipynb file to GitHub. I noticed that the charts are not showing up.
What I've tried:
# fix plotly is not showing in github.
# if you want to have interactive plotly, use `fig.show("notebook_connected")`
import plotly.io as pio
pio.renderers.default = "svg"
The problem is that the charts become not interactive even in local.
Changing it to pio.renderers.default = "svg+notebook", the charts will be interactive in local. But it won't show in GitHub.
stacks!
I'm trying to plot a graph using plotly, in Python. Specifically:
Plot: Maps, Bubblemaps, Choropleth..
Python: 3.9.6
Plotly: 4.01
Jupyter Notebook: 7.26
Env Manager: miniconda3
IDE: Ms. Visual Studio Code
I tried to run a code like this:
import plotly
import plotly.offline as pyo
pyo.init_notebook_mode()
import plotly.graph_objs as go
import plotly.express as ex
df = ex.data.gapminder().query("year==2007")
fig = ex.choropleth(df, locations="iso_alpha",
color="lifeExp", # lifeExp is a column of gapminder
hover_name="country", # column to add to hover information
color_continuous_scale=ex.colors.sequential.Plasma)
which is not my project code, just the example code of Plotly Map that I found on Plotly website (https://plotly.com/python/choropleth-maps/), but it's not showing anything.
P.S: I've tried to install plotly Jupyterlab dependencies from conda, i've also tried to use offline plotly but still nothing comes up. (Only the plot canvas and legend, the map is missing). Although, when I tried to make a bar chart it works. Help me masters!!!!!
I am using Jupyter lab, everything works fine within jupyter lab even when I am offline. However whenever I try to export the report to HTML. the plotly plots are not rendered. If I turn on my internet connection the plots are rendered, just fine.
Here is a sample code:
import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame(np.random.randn(100,4), columns='A B C D'.split())
px.scatter(df, x='A',y='B')
I have tried following the troubleshooting guide for plotly shown here. Additionally I tried installing on a fresh environment.
If i use the following:
import plotly.io as pio
pio.renderers.default = "jupyterlab"
The offline HTML includes static plot, however I would very like to have the interactivity enabled.
I have noticed that the files differs in size, the static pages are only around 700 Kb whereas when I try to save them as interactive they are about 4 Mb.
Is this not possible in Jupyter lab ? or am I missing something
If you want to be able to have interactivity while being offline, you need to add the plotly.js to the output html.
You can achieve that like this:
import plotly.io as pio
pio.renderers.default='notebook'
Actually, this should be done by default on JupyterLab (you can tell be the increased file size. As in your case it will be >4MB). So if that doesn't work, I suspect a bug. I think I've experienced something similar. Here's my browser console output when using your example and exporting it to html:
For some reason, the included plotly.js seems to depend on require.js which is not included in the html export for some reason. Instead, your page will try to load it from a CDN which fails when you are offline (as seen in the screenshot).
Now, what you can do is to manually include a local version of require.js. Get a copy here. Then, in your Notebook add the following at the top:
%%HTML
<script src="require.js"></script>
Then, export your notebook to html. Make sure, it is in the same folder as the require.js file you downloaded before and open it in the browser.
There should be no more error message in the console and your chart should appear and work interactively:
/e: If you want to share your notebook, this might be sub optimal as it requires you to also distribute the require.js script. You can also directly include the whole script in your notebook. Just put the %%js cell magic at the top of a code cell and paste the content of the require.js file you downloaded below that.
As you are trying to export it to HTML, don't forget jupyter's way (.html). Also with the "Open with" button on jupyter, you can see the maximum file size that it can handle. And most likely the storage wouldn't be the issue.
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:
The terminal looks like this after having run all the cells in the notebook once:
Can someone please point me to where I am going wrong here?
edit: I also posted this question on the plotly forums: https://community.plot.ly/t/no-output-from-plotly-offline-iplot/8086 -
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
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:
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.
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.
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
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.
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.
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
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.
On my side the culprit was the "Darker Jupyter" addon I was using on firefox.
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
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)
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')
Interactive matplotlib plotting is already a thing, but does not work properly in Pycharm, when used within a jupyter notebook.
The %matplotlib notebook does not work (throws no error, but I get <IPython ... JavaScript object> instead of a plot. If I plot normally (also with or without plt.show()) I just get a png and cannot interact in any way (even if, e.g., sliders are visible).
I couldn't find any answers elsewhere to this exact problem. It might be working in the browser version of jupyter, but I would like to stick to using PyCharm.
Pycharm v 2017.3 Community Edition
You can try:
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
Instead of importing only the peplos.
It's a trick I found on forum of Jetbrains
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000736584-SciView-in-PyCharm-2017-3-reduces-functionality-of-Matplotlib It works for me. With this, you skip actually the Sciview and plot in a normal matplotlib window