gpt4 book ai didi

python - 在 Bokeh 中根据 x_range 自动设置 vbar line_width

转载 作者:太空宇宙 更新时间:2023-11-03 14:58:48 30 4
gpt4 key购买 nike

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

理想情况下,宽度应始终与显示的条数成正比。我尝试查看 x_rangexaxis 上的各种属性,看看我是否可以获得范围并尝试自己计算宽度,但我没有找到任何有用的东西.一直在环顾四周,文档什么也没有。有什么想法吗?

最佳答案

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

因为我有一个函数来改变我如何分组我的列的freq和更新我的ColumnDataSource.data,我只需要重新计算宽度 当我更新它时。

这是工作代码:

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)

和我的更新函数:

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)

关于python - 在 Bokeh 中根据 x_range 自动设置 vbar line_width,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40185561/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com