如何在谷歌协作笔记本中显示输出?

How to display plotly outputs in google collaboratory notebooks?(如何在谷歌协作笔记本中显示输出?)
本文介绍了如何在谷歌协作笔记本中显示输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天搜索如何在 google colaboratory jupyter notebooks 中显示绘图的输出.google colaboratory 有一个 stackoverflow 问题和官方教程,但它们都对我不起作用.

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

stackoverflow 问题:
Plotly notebook 模式与 google colaboratory
https://colab.research.google.com/drive/14oudHx5e5r7hm1QcbZ24FVHXgVPD0k8f#scrollTo=8RCjUVpi

内置 google colaboratory plotly 版本为 1.12.12.

测试情节版本

绘图导入情节.__版本__1.12.12

加载库

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

安装谷歌驱动器

来自 google.colab 导入驱动器drive.mount('/content/drive')dat_dir = '驱动器/我的驱动器/Colab Notebooks/data/'

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

# 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 导入 iplot从 plotly.graph_objs 导入 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><脚本>需要js.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, iplot从 plotly.graph_objs 导入 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 或 cufflinks 适合您?

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

感谢您的帮助.

解决方案

plotly 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 中显示绘图图表,并偶然发现了这个线程,您在其中解释了网络上不同解决方案的问题.每个解决方案的感觉都是相同的.最后,当我看到 这个视频时,我的搜索结束了.p>

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

  1. 按照建议通过 !pip install plotly --upgraderestart runtime 在 colab 中升级 plotly.
  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 :)

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)