python - 如何使用 google collaboratory 在输出中显示 Plotly slider ?

标签 python slider plotly google-colaboratory

我想用 slider 重新创建这个 plotly 图:https://plot.ly/python/sliders/ 。使用 Colab 的这些说明:Plotly notebook mode with google colaboratory 。但是 slider 没有出现。

代码如下:

import numpy as np 
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot

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


init_notebook_mode(connected=True)

#from plotly sliders
data = [dict(
        visible = False,
        line=dict(color='#00CED1', width=6),
        name = '𝜈 = '+str(step),
        x = np.arange(0,10,0.01),
        y = np.sin(step*np.arange(0,10,0.01))) for step in np.arange(0,5,0.1)]
data[10]['visible'] = True


#configure added to visualize in colab
configure_plotly_browser_state()

steps = []
for i in range(len(data)):
    step = dict(
        method = 'restyle',  
        args = ['visible', [False] * len(data)],
    )
    step['args'][1][i] = True # Toggle i'th trace to "visible"
    steps.append(step)

sliders = [dict(
    active = 10,
    currentvalue = {"prefix": "Frequency: "},
    pad = {"t": 50},
    steps = steps
)]

layout = dict(sliders=sliders)

fig = dict(data=data, layout=layout)

#removed py. from original plotly code 
iplot(fig, filename='Sine Wave Slider')

代码运行并生成绘图。但是它不包括 slider 。我该如何包含它? 这是预期的输出: plotly slider plot 这是没有 slider 的输出图。 Plot without slider

最佳答案

错误在于版本不匹配。 slider 功能在 1.5.1 版本中不可用

您需要更改此特定代码行:

 paths: {
          base: '/static/base',
          plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext', 
        },

为了在窗口内获得最新版本的plotly。你可以看到working demo here

关于python - 如何使用 google collaboratory 在输出中显示 Plotly slider ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56585815/

相关文章:

python - OptionParser() 在 Python 中使用 --help 时显示选项

r - plotly 错误 "WebGL is not supported by your browser"

Python:如何使用 Plotly 堆叠或叠加直方图

python - Tkinter 将小部件放置在相对于窗口调整大小的特定位置

python - 在 App Engine 数据存储区中包含(而不是引用)一个模型到另一个模型中?

jquery - 如何反转 Owl Carousel slider 从右到左的滑动

jquery - 有没有适用于移动设备的 jquery ui slider ?

python - 如何从 3D 不规则数据中切片 2D 网格?

python - 如何使用 OpenCV 从多个摄像头捕获实时视频?

css - 链接到单选按钮的纯 CSS slider