本文介绍了Google colaboratory的Plotly Notebook模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用plotly笔记本模式进行协作-我打开一个新的笔记本,从plotly的文档中复制并粘贴以下简单示例,但看不到输出.通常情况下,输出空间中会有一个很大的空白.

I am am trying out colaboratory with plotly notebook mode - I open a new notebook, copy and paste the following simple example from plotly's documentation, but don't see an output. There is a large blank in the output space where the plot whould normally be.

这在我的本地笔记本上工作正常(这是plotly的较新版本,但根据他们的文档离线模式,该版本应可与google colab版本一起使用)有什么想法吗?

This works fine in my local notebook (which is a newer version of plotly, but per their docs offline mode should work with the google colab version)Any ideas?

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)中显示图形:

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版本3.x

这是一个示例,显示了在Colab中使用Plotly的情况. (通常需要自定义初始化.)

plotly version 3.x

Here's an example showing the use of Plotly in Colab. (Plotly requires custom initialization.)

https://colab.research.google.com/notebook#fileId=14oudHx5e5r7hm1QCgZ24 /a>

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

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

And call it in each offline plotting cell:

configure_plotly_browser_state()

这篇关于Google colaboratory的Plotly Notebook模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!