使用 google colaboratory 的 Plotly 笔记本模式

Plotly notebook mode with google colaboratory

我正在尝试使用 plotly notebook 模式进行协作 - 我打开一个新笔记本,从 plotly 的文档中复制并粘贴以下简单示例,但没有看到输出。输出 space 中通常是情节的地方有一大片空白。

这在我的本地笔记本中运行良好(这是 plotly 的较新版本,但根据他们的文档,离线模式应该适用于 google colab 版本) 有什么想法吗?

import plotly
from plotly.graph_objs import Scatter, Layout

plotly.offline.init_notebook_mode(connected=True)

plotly.offline.iplot({
    "data": [Scatter(x=[1, 2, 3, 4], y=[4, 3, 2, 1])],
    "layout": Layout(title="hello world")
})

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

这里有一个例子展示了在 Colab 中使用 Plotly。 (Plotly 需要自定义初始化。)

https://colab.research.google.com/notebook#fileId=14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f

你需要定义这个函数:

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-latest.min.js?noext',
            },
          });
        </script>
        '''))

并在每个离线绘图单元中调用它:

configure_plotly_browser_state()

configure_plotly_browser_state() 可以在每个单元格 运行 之前执行,方法是使用 IPython 的 pre_run_cell 钩子:

import IPython

IPython.get_ipython().events.register('pre_run_cell', configure_plotly_browser_state)

您需要添加一个方法才能在 Colab 中使用 Plotly。

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)

必须对显示 Plotly 图形的每个单元格执行此方法。

样本:

from plotly.offline import iplot
import plotly.graph_objs as go

enable_plotly_in_cell()

data = [
    go.Contour(
        z=[[10, 10.625, 12.5, 15.625, 20],
           [5.625, 6.25, 8.125, 11.25, 15.625],
           [2.5, 3.125, 5., 8.125, 12.5],
           [0.625, 1.25, 3.125, 6.25, 10.625],
           [0, 0.625, 2.5, 5.625, 10]]
    )
]
iplot(data)

参考:https://colab.research.google.com/notebooks/charts.ipynb#scrollTo=WWbPMtDkO4xg

解决方案

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

您需要更改默认渲染。这是文档的摘录。 https://plot.ly/python/renderers/#setting-the-default-renderer

The current and available renderers are configured using the plotly.io.renderers configuration object. Display this object to see the current default renderer and the list of all available renderers.

>>>import plotly.io as pio
>>>pio.renderers


Renderers configuration
    -----------------------
        Default renderer: 'notebook_connected'
        Available renderers:
            ['plotly_mimetype', 'jupyterlab', 'nteract', 'vscode',
             'notebook', 'notebook_connected', 'kaggle', 'azure', 'colab',
             'cocalc', 'databricks', 'json', 'png', 'jpeg', 'jpg', 'svg',
             'pdf', 'browser', 'firefox', 'chrome', 'chromium', 'iframe',
             'iframe_connected', 'sphinx_gallery']

只需将"colab"作为参数renderer的值传递给fig.show(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")