当布局放入选项卡中时,散布绘图彼此呈现



我有一个包含散景列表的字典。我正在称呼这些并将2个图放在布局上,然后将它们放入3或4个选项卡中。

打开文档时,每个选项卡看起来只有一个图,但是您可以说工具栏后面有第二个图,有时您可以看到第二个图的小部分。

当我只有show(layout)时,它看起来不错,只有当我尝试在选项卡中放置布局时,它才能正确渲染。

我已经创建了问题。下面有点长,但是我想有一个完整的例子。第一部分只是创建所有图。请注意需要更改的目录路径。

from bokeh.charts import Bar, output_file, show, BoxPlot, Histogram, Scatter
from bokeh.sampledata.autompg import autompg as df
from bokeh.models.widgets import Tabs, Panel
from bokeh.layouts import layout
import os
directory = r'/Users/user/bokehApp'

bar = []
p = Bar(df, 'cyl', values='mpg', title="Total MPG by CYL")
bar.append(p)
p = Bar(df, label='yr', values='mpg', agg='mean',
        title="Average MPG by YR")
bar.append(p)
p = Bar(df, 'yr', values='displ',
        title="Total DISPL by YR", bar_width=0.4)
bar.append(p)
p = Bar(df, 'yr', values='displ',
        title="Total DISPL by YR", color="wheat")
bar.append(p)
box = []
p = BoxPlot(df, values='mpg', label='cyl',
            title="MPG Summary (grouped by CYL)")
box.append(p)
p = BoxPlot(df, values='mpg', label=['cyl', 'origin'],
            title="MPG Summary (grouped by CYL, ORIGIN)")
box.append(p)
p = BoxPlot(df, values='mpg', label='cyl', color='#00cccc',
            title="MPG Summary (grouped by CYL)")
box.append(p)
p = BoxPlot(df, values='mpg', label='cyl', color='cyl',
            title="MPG Summary (grouped and shaded by CYL)")
box.append(p)
hist = []
p = Histogram(df['mpg'], title="MPG Distribution")
hist.append(p)
p = Histogram(df, 'hp', title="HP Distributioan")
hist.append(p)
p = Histogram(df, values='displ', title="DISPL Distribution")
hist.append(p)
p = Histogram(df, values='mpg', bins=50,
              title="MPG Distribution (50 bins)")
hist.append(p)
scat = []
p = Scatter(df, x='mpg', y='hp', title="HP vs MPG",
            xlabel="Miles Per Gallon", ylabel="Horsepower")
scat.append(p)

以上是我从高级图表页面上获取的所有图,以下是问题。

dataDict = {'Bar': bar, 'Box': box, 'Hist': hist, 'Scat': scat}
plots = ['Bar', 'Box', 'Hist']
for plt in plots:
    plotFig = dataDict[plt]
    tabList = [plotFig[0:2],plotFig[2:4]]
    tabTitle =['tab1', 'tab2']
    panel = []
    output_file(os.path.join(directory, plt+'.html'), title = plt + 'plots', autosave = False, mode = 'cdn', root_dir = None)
    for tab, title in zip (tabList, tabTitle):
        l = layout(children = [
                   tab], sizing_mode = 'scale_width')
        t = Panel(child = l, title = title)
        panel.append(t)
    tabs = Tabs(tabs = panel)
    show(tabs)

谢谢

在这种情况下,从您的描述中不是100%确定您的含义。但是Bokeh保持了一个隐式的"当前文档",该文档被积累到。如果您不想要这个(例如,因为您在循环中创建和保存不同的图),则可以:

  • 管理文档并明确地向他们添加东西,例如https://github.com/bokeh/bokeh/blob/master/master/examples/models/glyph1.py

  • 尝试在呼叫show之间使用reset_output

    from bokeh.io import reset_output
    for p in plots:
        reset_output()
        # stuff, create plots and put them in Tabs
        show(tabs)
    

当我将上述sizing_mode = 'scale_width'更改为 sizing_mode = 'fixed'时,它正确渲染。

最新更新