如何在 google 协作笔记本中显示 plotly 输出?

How to display plotly outputs in google collaboratory notebooks?

我搜索了一整天如何在 google colaboratory jupyter notebooks 中显示 plotly plots 的输出。 google colaboratory 有一个 Whosebug 问题和官方教程,但它们都不适合我。

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

Whosebug 问题:

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

装载google驱动器

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

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

官方google合作实验室方法(失败)

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

测试官方建议(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)

Whosebug Bob Smith 方法

# 
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 或 cufflinks 适合您?

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

感谢您的帮助。

plotly版本4.x

从版本 4 开始,plotly 渲染器了解 Colab,因此以下内容足以在 Colab 和 Jupyter(以及其他笔记本,如 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版本3.x

我也在努力在 Google colab 中显示绘图,偶然发现了这个线程,您在其中通过网络解释了不同解决方案的问题。每个解决方案的感觉都是一样的。最后,当我遇到 this video.

时,我的搜索结束了

我遵循了他的方法(可能与您已经尝试过的方法相似)并且这对我有用。

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

    configure_plotly_browser_state()

    init_notebook_mode(已连接=假)

    iplot(XXXXXX)

只需导入 plotly 库

如果有帮助请告诉我:)

使用 plot 而不是 iplot ...我花了一段时间才弄明白。您可以将绘图同时写入笔记本和 gdrive。

在笔记本的开头添加行'%matplotlib inline'

参考下面link: https://github.com/jupyter/notebook/issues/3523

尝试使用renderer="colab"如下图:

import plotly.graph_objects as go
fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displayed with the 'colab' Renderer"
)
fig.show(renderer="colab")

使用 import plotly.io as pio pio.renderers.default = "colab"

它适用于 plotly 版本 5.5.0