如何在谷歌合作笔记本中显示情节输出? [英] How to display plotly outputs in google collaboratory notebooks?

查看:26
本文介绍了如何在谷歌合作笔记本中显示情节输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了一整天如何在 google colaboratory jupyter notebooks 中显示情节图的输出.有一个 stackoverflow question 和来自 google colaboratory 的官方教程,但它们都不适合我.

官方链接:
https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=hFCg8XrdO4xj

stackoverflow 问题:
与 google colaboratory 一起绘制笔记本模式
https://colab.research.google.com/drive/14oudHx1QFVc8cTouFulls7dhx1QFVc8hx2Vc8rx7Vc8dhx1Qfvc8hx2Vc7vpd0k8f#scrollTo=8RCjUVpi2_xd

内置的 google colaboratory plotly 版本是 1.12.12.

测试情节版本

plotly 导入plotly.__version__1.12.12

加载库

将 numpy 导入为 np将熊猫导入为 pd导入 matplotlib.pyplot 作为 plt将 seaborn 作为 sns 导入

安装谷歌驱动器

来自 google.colab 导入驱动器drive.mount('/内容/驱动器')dat_dir = '驱动器/我的驱动器/Colab 笔记本/数据/'

官方谷歌合作方法(失败)

# https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=hFCg8XrdO4xjdef enable_plotly_in_cell():导入 IPython从 plotly.offline 导入 init_notebook_mode显示(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script>'''))init_notebook_mode(连接=假)

测试官方建议(失败)

import plotly.plotly as py将 numpy 导入为 np从 plotly.offline 导入 iplotfrom plotly.graph_objs import Contours, Histogram2dContour, Marker, Scatterenable_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-colaboratorydef configure_plotly_browser_state():导入 IPython显示(IPython.core.display.HTML('''<script src="/static/components/requirejs/require.js"></script><脚本>requirejs.config({路径:{基地:'/静态/基地',情节:'https://cdn.plot.ly/plotly-1.5.1.min.js?noext',},});'''))

测试 Bob Smith 方法(失败)

# https://colab.research.google.com/drive/14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f#scrollTo=8RCjUVpi2_xd导入 plotly.plotly 作为 py将 numpy 导入为 np从 plotly.offline 导入 init_notebook_mode, iplotfrom plotly.graph_objs import Contours, Histogram2dContour, Marker, Scatterconfigure_plotly_browser_state()init_notebook_mode(连接=假)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 或袖扣适合您?

如果不能显示,能否将输出文件保存为.html在我们的google驱动器中手动打开看看?

感谢您的帮助.

解决方案

plotly version 4.x

从第 4 版开始,plotly 渲染器了解 Colab,因此以下内容足以在 Colab 和 Jupyter(以及 Kaggle、Azure、nteract 等其他笔记本)中显示图形:

按原样导入 plotly.graph_objectsfig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2]) )图.show()

plotly 3.x 版

我也在努力在 Google colab 中显示情节图,并偶然发现了这个帖子,您通过网络解释了不同解决方案的问题.每种解决方案的感受都是一样的.最后,当我看到这个视频时,我的搜索结束了.>

我遵循了他的方法(可能与您已经尝试过的方法类似),这对我有用.

  1. 按照建议在 colab 中通过 !pip install plotly --upgrade重新启动运行时 进行绘图升级.
  2. 评论重新运行笔记本之前的升级选项
  3. 定义函数configure_plotly_browser_state()
  4. 调用 plotly 库
  5. 在要调用 iplot 的每个单元格中调用如下所示的函数和笔记本模式

    configure_plotly_browser_state()

    init_notebook_mode(connected=False)

    iplot(XXXXXX)

只需导入 plotly 库

如果这有帮助,请告诉我:)

I searched whole day how to display the outputs of plotly plots in google colaboratory jupyter notebooks. There is a stackoverflow question and also official tutorial from google colaboratory but both of them did not work for me.

official link:
https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=hFCg8XrdO4xj

stackoverflow question:
Plotly notebook mode with google colaboratory
https://colab.research.google.com/drive/14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f#scrollTo=8RCjUVpi2_xd

The built-in google colaboratory plotly version is 1.12.12.

Test plotly version

import plotly
plotly.__version__
1.12.12

Load libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

Mount google drive

from google.colab import drive
drive.mount('/content/drive')

dat_dir = 'drive/My Drive/Colab Notebooks/data/'

Official google colaboratory method (FAILED)

# 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)

Test official suggestion (FAILED)

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 Method

# 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>
        '''))

Test Bob Smith method (FAILED)

# 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)

Questions

How to display the plotly output in google colaboratory?

Is is possible ? If so which version of plotly or cufflinks is working for you?

If it is not possible to display, can we save the output file as .html in our google drive and open them manually and see them?

I appreciate your help.

解决方案

plotly version 4.x

As of version 4, plotly renderers know about Colab, so the following is sufficient to display a figure in both Colab and Jupyter (and other notebooks like Kaggle, Azure, nteract):

import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
fig.show()

plotly version 3.x

I was also struggling to display the plotly graphs in Google colab and stumbled upon this thread where you explained the problems with different solutions over the net. Feelings are the same for each one of the solutions. Finally, my search ended when I came across this video.

I followed his approach (might be similar to the ones you already tried) and this worked for me.

  1. Upgrade plotly in colab thru !pip install plotly --upgrade and restart runtime as suggested.
  2. Comment the upgrade option before re-running your notebook
  3. Define the function configure_plotly_browser_state()
  4. Invoke plotly libraries
  5. Call function and notebook mode like below in every cell where you want to call iplot

    configure_plotly_browser_state()

    init_notebook_mode(connected=False)

    iplot(XXXXXX)

Just import plotly libraries

Please let me know if this helps :)

这篇关于如何在谷歌合作笔记本中显示情节输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆