【发布时间】:2019-05-30 10:28:49
【问题描述】:
我整天搜索如何在 google colaboratory jupyter notebooks 中显示绘图的输出。 google colaboratory 有一个 stackoverflow 问题和官方教程,但它们都对我不起作用。
官方链接:
https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=hFCg8XrdO4xj
stackoverflow 问题:
Plotly notebook mode with google colaboratory
https://colab.research.google.com/drive/14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f#scrollTo=8RCjUVpi2_xd
内置 google colaboratory plotly 版本为 1.12.12。
测试情节版本
import plotly
plotly.__version__
1.12.12
加载库
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
安装谷歌驱动器
from google.colab import drive
drive.mount('/content/drive')
dat_dir = 'drive/My Drive/Colab Notebooks/data/'
谷歌官方合作方法(失败)
# https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=hFCg8XrdO4xj
def enable_plotly_in_cell():
import IPython
from plotly.offline import init_notebook_mode
display(IPython.core.display.HTML('''
<script src="/static/components/requirejs/require.js"></script>
'''))
init_notebook_mode(connected=False)
测试官方建议(失败)
import plotly.plotly as py
import numpy as np
from plotly.offline import iplot
from plotly.graph_objs import Contours, Histogram2dContour, Marker, Scatter
enable_plotly_in_cell()
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
Stackoverflow Bob Smith 方法
# https://stackoverflow.com/questions/47230817/plotly-notebook-mode-with-google-colaboratory
def configure_plotly_browser_state():
import IPython
display(IPython.core.display.HTML('''
<script src="/static/components/requirejs/require.js"></script>
<script>
requirejs.config({
paths: {
base: '/static/base',
plotly: 'https://cdn.plot.ly/plotly-1.5.1.min.js?noext',
},
});
</script>
'''))
测试 Bob Smith 方法(失败)
# https://colab.research.google.com/drive/14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f#scrollTo=8RCjUVpi2_xd
import plotly.plotly as py
import numpy as np
from plotly.offline import init_notebook_mode, iplot
from plotly.graph_objs import Contours, Histogram2dContour, Marker, Scatter
configure_plotly_browser_state()
init_notebook_mode(connected=False)
x = np.random.randn(2000)
y = np.random.randn(2000)
iplot([Histogram2dContour(x=x, y=y, contours=Contours(coloring='heatmap')),
Scatter(x=x, y=y, mode='markers', marker=Marker(color='white', size=3, opacity=0.3))], show_link=False)
问题
如何在 google colaboratory 中显示 plotly 输出?
有可能吗?如果是这样,哪个版本的 plotly 或 cufflinks 适合您?
如果无法显示,能否将输出文件保存为.html在我们的google drive中手动打开查看?
感谢您的帮助。
【问题讨论】:
-
我测试了 Bob Smith 的笔记本,它运行良好。
-
@KorakotChaovavanich 很高兴知道,你的情节和/或袖扣版本是什么?你的意思是工作意味着显示图像还是只是工作?代码对我来说也很好,只是没有在笔记本上显示任何东西,代码没有失败,只有不显示!
-
当我运行笔记本 colab.research.google.com/drive/… 时,它正常显示。在运行之前,您可能需要制作笔记本的副本,或“在操场上打开”。
标签: python jupyter-notebook plotly google-colaboratory