从 Bokeh 图形中删除填充或边距

Removing padding or margins from Bokeh Figures

是否可以去除散景图像两侧的填充(或边距)? 我一直在看各种数字,它们的左边似乎都有这个空白区域。我也试过改变边界 例如:s1.min_border_left = 0 or change it to s1.min_border_left = 40

但它似乎并没有完成这项工作,它改变了边框,但不知何故似乎有一个固定且不可更改的填充,这是我想摆脱的区域的一个例子: Image example 那么有没有可能摆脱它,只让图形贴在浏览器的左侧?

Bokeh Figures and Borders

打开生成的 HTML 页面并将宽度 90% 替换为 100%

如果使用散景服务器,您可以添加一个外部 css sheet 这样的东西来修改边距。

html, body {
    display: block;
    margin: 0 auto;
    width: 99%;
} 

根据其他答案,以下是可能会派上用场的片段:

import os
import re
import tempfile
import webbrowser
import time

from bokeh.io import output_file, save


def save_bokeh_nomargins(plot, filename):
    """
    Saves a bokeh plot/layout without having the left margin.
    """
    # Strategy:
    #  1. save the plot to a temp file
    #  2. change the offending line of code
    #  3. save the result to the destination file
    ftemp = tempfile.mktemp()
    try:
        output_file(ftemp)
        save(plot)
        with open(ftemp) as ftemph, open(filename, "w") as fouth:
            fouth.write(re.sub("width: 90%;", "width: 100%;", ftemph.read(), count=1))
    finally:
            os.remove(ftemp)


def show_bokeh_nomargins(plot):
    """
    Displays a bokeh plot/layout without having the left margin.
    """
    ftemp = tempfile.mktemp()
    try:
        save_bokeh_nomargins(plot, ftemp)
        webbrowser.open(ftemp)
        time.sleep(1)
    finally:
        os.remove(ftemp)

我要注意,这有点老套,如果你在文件的其他地方有 width: 90%,它可能会出错。