[英]plotly.offline.iplot gives a large blank field as its output in Jupyter Notebook/Lab
我正在尝试在 Jupyter 笔记本中创建一个 Sankey 图表,我的代码基于此处显示的第一个示例。
我最终得到了这个,我可以运行它而不会出现任何错误:
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 ply.offline.iplot(fig, filename='Test')
的最终 output 只显示一个大的空白字段: 运行一次笔记本中的所有单元格后,终端看起来像这样:
有人可以指出我在这里出错的地方吗?
过去,我在 Jupyter 中遇到了类似的离线情节问题 - 有时当/为什么情节没有出现时,它会令人惊讶地不一致。 从增加的数据速率限制开始可能值得一试。
jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10
我尝试了这里建议的所有解决方案,但没有一个对我有用。 解决问题的是添加:
import plotly.io as pio
pio.renderers.default='notebook'
并且还使用fig.show("notebook")
而不是简单地fig.show(),作为建议在这里。
使用 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 样式并显示绘图。
希望这可以帮助。
如果这些答案都不适合您。 试试这个
只需在以管理员权限打开的 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
server 获得正确的显示(没有任何其他选项),但使用jupyter lab
server 获得一个空白块。 相关版本信息:
$ jupyter lab --version
0.35.5
$ jupyter notebook --version
5.7.8
$ python -c "import plotly; print(plotly.__version__)"
3.10.0
所以对于那些使用JupyterLab 的人,为了在JupyterLab 中正确显示离线plotly
,我们需要使用以下命令安装plotly-extension
(以下摘自相关答案):
$ jupyter labextension install @jupyterlab/plotly-extension
$ jupyter labextension list
$ jupyter lab build
这显示了一个数字,而这里的其他答案都不适合我:
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 安装
我自己也遇到过类似的问题,有一个旭日图。 空白图是由于无效数据。 我原以为在运行时会出现错误,但似乎在某些极少数情况下,不会引发错误,而是显示空白图。
因此,请检查您的数据有效性,或使用 plotly doc 上提供的虚拟数据进行测试,以测试问题是否来自数据或 plotly/notebook 界面。
上述一些方法确实对我有用。 但是我根据这个讨论和官方的故障排除指南不及时地解决了这种问题。
$ conda remove plotly
$ jupyter labextension uninstall jupyterlab-plotly
$ jupyter labextension uninstall plotlywidget
$ jupyter lab build
$ conda install plotly
$ jupyter labextension install jupyterlab-plotly
$ jupyter labextension install plotlywidget
以上程序在jupyter lab
对我来说很好用。 我认为我们可以在jupyter notebook
尝试相同的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 中尝试 plot 热图时,我几乎遇到了类似的问题,对我来说,以下解决方案有所帮助:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.