首页 > 解决方案 > 与谷歌 colab 合作,

问题描述

我正在 colab 上重新运行我的笔记本,而 plotly 的 fig.show 似乎不再起作用我尝试安装另一个 plotly 版本(我有 4.4.1),但我没有成功获得我的身材

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")
#fig.show()

我什么都没看到。我需要将 HTML 与 HTML(fig.to_html()) 一起使用,但这不是解决方案,因为它在使用小部件后不起作用

你有什么主意吗 ?

我看到这篇文章Plotly notebook mode with google colaboratory但即使使用渲染器我也没有成功绘制任何东西

编辑:解决方案

  1. 定义
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)
  1. 然后在要绘制的任何单元格中调用此函数
#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"
)
enable_plotly_in_cell()
fig.show()

标签: plotlygoogle-colaboratory

解决方案


推荐阅读