数据块中散景的循环

For loop for bokeh in databricks

嗨,我想遍历 for 循环,例如。几次下面的代码在一个数据块单元格中获得几次相同的图表:

我导入了库:

from bokeh.plotting import figure
from bokeh.embed import components, file_html
from bokeh.resources import CDN

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]


for i in range(5):
p = figure(title='test', x_axis_label = 'x values', y_axis_label='y values')
p.line(x,y, line_width =2)
html = file_html(p,CDN,'plot')
displayHTML(html)

我试图使用 for 循环,但我仍然只能在一个单元格中获得一个图形。

还尝试了不同的模块,如下所示:

from bokeh.io import output_file, show
from bokeh.plotting import figure

x = [1, 2, 3, 4, 5]
y = [6, 7, 2, 4, 5]

for i in range(5):
  p = figure(title='test', x_axis_label = 'x values')
  p.line(x,y, line_width =2)
  output_file("line"+str(i)+".html")
  show(p)

但在这里我没有得到任何结果,没有绘制任何东西。你能告诉我为什么吗?

也试过这个:

d={}
for i in range(5):
  p = figure(title='test', x_axis_label = 'x values')
  p.line(x,y, line_width =2)
  d["html{0}".format(i)]=file_html(p,CDN, 'plot' + str(i))

for j in d:
  displayHTML(j)

看起来在数据块中使用散景可以每个单元格只显示一个图形。有人知道这是不是真的吗?

你能帮我搞定 for 循环多次获取的语法吗?

从其他答案看来,使用 Bokeh 不完全支持的旧笔记本版本的 Databricks 可能存在问题。如果标准 output_notebook 不起作用,但 displayHTML 起作用,那么我会说你最好的选择是收集要在 column 布局中显示的图,然后显示最后一次调用 show 就可以同时调用它们:

from bokeh.layouts import column

plots = []
for i in range(5):
    p = figure(...)
    p.line(...)
    plots.append(p)

layout = column(*plots)

# only use this way for databricks
html = file_html(layout, CDN, 'plot')
displayHTML(html)