如何在Google合作笔记本中显示绘图输出? [英] How to display plotly outputs in google collaboratory notebooks?

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

问题描述

我整天都在搜索如何在Google colajuatory jupyter笔记本中显示绘图的输出.有一个stackoverflow问题,还有来自google colaboratory的官方教程,但它们都不对我有用.

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

stackoverflow问题:
将笔记本模式与Google colaboratory配合使用
>://colab.research.google.com/drive/14oudHxHx5r5Z7PD2VJ2C

内置的Google colaplan绘图版本是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)

测试官方建议(失败)

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鲍勃·史密斯方法

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

测试鲍勃·史密斯方法(失败)

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

如果无法显示,我们可以将输出文件另存为.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中显示绘图图,偶然发现了该线程,在此您通过网络解释了不同解决方案的问题.每个解决方案的感受都是相同的.最后,当我遇到这部视频时,搜索结束.

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

  1. 根据建议通过!pip install plotly --upgrade 重新启动运行时,在colab中进行有计划的升级.
  2. 评论升级选项,然后重新运行笔记本电脑
  3. 定义功能 configure_plotly_browser_state ()
  4. 调用绘图库
  5. 在要调用iplot的每个单元格中,如下所示调用函数和笔记本模式

    configure_plotly_browser_state()

    init_notebook_mode(connected = False)

    iplot(XXXXXX)

只需导入剧情库

请告诉我这是否有帮助:)

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

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

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