plotly.offline.iplot 在 Jupyter 中给出一个大的空白字段作为输出 Notebook/Lab
plotly.offline.iplot gives a large blank field as its output in Jupyter Notebook/Lab
我正在尝试在 Jupyter notebook 中创建一个 Sankey 图表,我的代码基于 the first example shown here。
我最终得到了这个,我可以 运行 而不会出现任何错误:
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')
csv 文件如下所示:
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
看起来 运行 很好,各种输出 乍一看 是对的,但是 ply.offline.iplot(fig, filename='Test')
的最终输出只是显示了一个大的空白字段:
在笔记本中的所有单元格一次 运行 之后,终端看起来像这样:
谁能指出我哪里出错了?
- 编辑:我还在 plotly 论坛上发布了这个问题:https://community.plot.ly/t/no-output-from-plotly-offline-iplot/8086 -
我过去在 Jupyter 中遇到过类似的离线绘图问题 - 有时它出人意料地不一致 when/why 绘图无法显示。从提高数据速率限制开始可能值得一试。
jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10
对我来说,帮助将笔记本更改为 Trusted
(这启用了 Javascript 并提供了将图形构建到 jupyter 笔记本中的巧妙方法。)。
您可以在此处找到此选项:
这显示了一个数字,而这里的 none 其他答案对我有用:
import plotly.graph_objs as go
fig = go.FigureWidget()
# Display an empty figure
fig
这(在新单元格中)用条形图和折线图修改了上面的图:
# 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'
如果这不起作用,请确保您的安装正确,例如pip install --user --upgrade plotly
如果你用 pip 安装
如果这些答案中的 none 对您有用。试试这个
只需在以管理员权限打开的 conda 提示符中执行此操作
pip install pip --upgrade
conda upgrade notebook or pip install notebook --upgrade
conda install -c conda-forge ipywidgets or pip install ipywidgets
您需要 jupyter notebook
和 ipywidgets
的最新版本。
我可以使用 jupyter notebook
服务器获得正确的显示(没有任何附加选项),但是得到一个空白块
使用 jupyter lab
服务器 。相关版本信息:
$ jupyter lab --version
0.35.5
$ jupyter notebook --version
5.7.8
$ python -c "import plotly; print(plotly.__version__)"
3.10.0
因此,对于那些使用 JupyterLab 的人来说,要在 中正确显示离线 plotly
图表JupyterLab,我们需要使用以下命令安装plotly-extension
(以下摘自):
$ jupyter labextension install @jupyterlab/plotly-extension
$ jupyter labextension list
$ jupyter lab build
使用 Google Colab 时,包含代码段 -
import plotly.io as pio
pio.renderers.default = 'colab'
或将整体导入语句用作 -
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
这会将渲染设置为 Colab 样式并显示绘图。
希望对您有所帮助。
我尝试了此处建议的所有解决方案,但 none 对我有用。解决问题的方法是添加:
import plotly.io as pio
pio.renderers.default='notebook'
并且还使用 fig.show("notebook")
而不是简单地 fig.show(),如 here 所建议的那样。
我自己也遇到过类似的问题,有旭日图。空白图是由于无效数据。我本以为会在运行时出错,但在极少数情况下似乎没有出现错误,而是显示空白图。
因此,请检查您的数据有效性,或使用 plotly doc 上提供的虚拟数据进行测试,以测试问题是否来自数据或 plotly/notebook 界面。
上面的一些方法对我有用。但是我按照这个discussion and the official troubleshooting guide.
很不灵巧地解决了这类问题
- 第一步:
关闭你的 jupyter 实验室。然后,卸载 plotly 和一些扩展。
$ conda remove plotly
$ jupyter labextension uninstall jupyterlab-plotly
$ jupyter labextension uninstall plotlywidget
- 第 2 步:在与启动 JupyterLab 相同的环境中打开终端 以重建 jupyter lab。
$ jupyter lab build
- 第 3 步:使用相同版本
重新安装 plotly 和 jupyter lab 扩展
$ conda install plotly
$ jupyter labextension install jupyterlab-plotly
$ jupyter labextension install plotlywidget
以上程序在 jupyter lab
中对我来说工作正常。我认为我们可以在 jupyter notebook
.
中尝试相同的程序
在我这边,罪魁祸首是我在 firefox 上使用的“Darker Jupyter”插件。
包括这些行:
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
适合我
我的回复可能有点晚了,但与 Plotly 一起导入也很重要 plotly.graph。
这些是您需要导入的包。我也得到了空白输出,这对我有用。
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
我在尝试在 colab 中绘制热图时遇到了几乎类似的问题,对我来说,以下解决方案有所帮助:
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')
我正在尝试在 Jupyter notebook 中创建一个 Sankey 图表,我的代码基于 the first example shown here。
我最终得到了这个,我可以 运行 而不会出现任何错误:
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')
csv 文件如下所示:
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
看起来 运行 很好,各种输出 乍一看 是对的,但是 ply.offline.iplot(fig, filename='Test')
的最终输出只是显示了一个大的空白字段:
谁能指出我哪里出错了?
- 编辑:我还在 plotly 论坛上发布了这个问题:https://community.plot.ly/t/no-output-from-plotly-offline-iplot/8086 -
我过去在 Jupyter 中遇到过类似的离线绘图问题 - 有时它出人意料地不一致 when/why 绘图无法显示。从提高数据速率限制开始可能值得一试。
jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10
对我来说,帮助将笔记本更改为 Trusted
(这启用了 Javascript 并提供了将图形构建到 jupyter 笔记本中的巧妙方法。)。
您可以在此处找到此选项:
这显示了一个数字,而这里的 none 其他答案对我有用:
import plotly.graph_objs as go
fig = go.FigureWidget()
# Display an empty figure
fig
这(在新单元格中)用条形图和折线图修改了上面的图:
# 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'
如果这不起作用,请确保您的安装正确,例如pip install --user --upgrade plotly
如果你用 pip 安装
如果这些答案中的 none 对您有用。试试这个
只需在以管理员权限打开的 conda 提示符中执行此操作
pip install pip --upgrade
conda upgrade notebook or pip install notebook --upgrade
conda install -c conda-forge ipywidgets or pip install ipywidgets
您需要 jupyter notebook
和 ipywidgets
的最新版本。
我可以使用 jupyter notebook
服务器获得正确的显示(没有任何附加选项),但是得到一个空白块
使用 jupyter lab
服务器 。相关版本信息:
$ jupyter lab --version
0.35.5
$ jupyter notebook --version
5.7.8
$ python -c "import plotly; print(plotly.__version__)"
3.10.0
因此,对于那些使用 JupyterLab 的人来说,要在 中正确显示离线 plotly
图表JupyterLab,我们需要使用以下命令安装plotly-extension
(以下摘自
$ jupyter labextension install @jupyterlab/plotly-extension
$ jupyter labextension list
$ jupyter lab build
使用 Google Colab 时,包含代码段 -
import plotly.io as pio
pio.renderers.default = 'colab'
或将整体导入语句用作 -
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
这会将渲染设置为 Colab 样式并显示绘图。
希望对您有所帮助。
我尝试了此处建议的所有解决方案,但 none 对我有用。解决问题的方法是添加:
import plotly.io as pio
pio.renderers.default='notebook'
并且还使用 fig.show("notebook")
而不是简单地 fig.show(),如 here 所建议的那样。
我自己也遇到过类似的问题,有旭日图。空白图是由于无效数据。我本以为会在运行时出错,但在极少数情况下似乎没有出现错误,而是显示空白图。
因此,请检查您的数据有效性,或使用 plotly doc 上提供的虚拟数据进行测试,以测试问题是否来自数据或 plotly/notebook 界面。
上面的一些方法对我有用。但是我按照这个discussion and the official troubleshooting guide.
很不灵巧地解决了这类问题- 第一步: 关闭你的 jupyter 实验室。然后,卸载 plotly 和一些扩展。
$ conda remove plotly
$ jupyter labextension uninstall jupyterlab-plotly
$ jupyter labextension uninstall plotlywidget
- 第 2 步:在与启动 JupyterLab 相同的环境中打开终端 以重建 jupyter lab。
$ jupyter lab build
- 第 3 步:使用相同版本 重新安装 plotly 和 jupyter lab 扩展
$ conda install plotly
$ jupyter labextension install jupyterlab-plotly
$ jupyter labextension install plotlywidget
以上程序在 jupyter lab
中对我来说工作正常。我认为我们可以在 jupyter notebook
.
在我这边,罪魁祸首是我在 firefox 上使用的“Darker Jupyter”插件。
包括这些行:
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
适合我
我的回复可能有点晚了,但与 Plotly 一起导入也很重要 plotly.graph。
这些是您需要导入的包。我也得到了空白输出,这对我有用。
from plotly.offline import plot, iplot, init_notebook_mode
import plotly.graph_objs as go
init_notebook_mode(connected=True)
我在尝试在 colab 中绘制热图时遇到了几乎类似的问题,对我来说,以下解决方案有所帮助:
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')