根据散景中的 x_range 自动设置 vbar line_width

aig*_*fer 3 python graph bokeh

我有一个vbar绑定到ColumnDataSource根据一些小部件选择更新的。如果我从line_width=5我的初始数据开始,它看起来很棒。但是,当我更新图形时,x_range会更新以适应更新的数据并导致条形的相对宽度发生变化。

理想情况下,宽度应始终与显示的条形数量成正比。我试图寻找在对各种属性x_rangexaxis看如果我能得到的范围内,并尝试计算宽度自己,但我没有发现任何帮助。一直在环顾四周和文档,什么也没有。有什么想法吗?

aig*_*fer 5

我终于在@bigreddot 的帮助下解决了这个问题。事实证明我使用了错误的属性。而不是使用line_width我需要使用width. 由于 myx_range是一个datetime范围,并且datetimes以毫秒为单位表示,因此我需要足够大的宽度才能正确显示。这负责在放大时设置比例宽度,因为宽度代表x_axis.

由于我有一个函数可以更改freq我对列进行分组和更新我的方式ColumnDataSource.data,因此我只需要width在更新它时重新计算。

这是工作代码:

def get_data(freq='MS'):
    return pd.DataFrame(srs.groupby(pd.Grouper(freq=freq)).mean())

source = ColumnDataSource(data=ColumnDataSource.from_df(get_data()))

def get_width():
    mindate = min(source.data['date'])
    maxdate = max(source.data['date'])
    return 0.8 * (maxdate-mindate).total_seconds()*1000 / len(source.data['date'])

f = figure(plot_width=550, plot_height=400, x_axis_type="datetime")
f.x_range.set(bounds='auto')
r = f.vbar(source=source, top='volume', x='date', width=get_width())
bar_glyph = f.renderers[-1].glyph

handle = show(f, notebook_handle=True)
Run Code Online (Sandbox Code Playgroud)

和我的更新功能:

def update_data(freq={'Quarter': 'QS', 'Month': 'MS', 'Week': 'W'}):
    source.data = ColumnDataSource.from_df(get_data(freq))
    r.glyph.width = get_width()
    push_notebook()

i = interact(update_data)
Run Code Online (Sandbox Code Playgroud)