You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bokeh矩形Glyph使用问题:宽度无效与X轴共享导致间隙

Fixing Large Gaps Between Rectangles in Bokeh 1.4.0

Hey there! Let’s tackle that frustrating gap issue with your Bokeh rect plots for anomaly alerts. I’ve run into similar datetime axis quirks before, so here’s what to check and fix:

1. First, Check the width Parameter’s Units

Since your x-axis is set to datetime, Bokeh interprets the width value as milliseconds. If you’re using width=1000, that’s only 1 second wide—way too small if your data points are spaced minutes or hours apart.

For example, if your estampilles data is sampled every hour, set width to ~3.2 million (3600000 * 0.9, leaving a small gap between rects):

# Adjust based on your actual data interval
data_interval_ms = 3600 * 1000  # 1 hour in ms
for j, name in zip(range(len(anomaly_names)), anomaly_names):
    p4.rect(
        x="estampilles", 
        y=heatmap_y_coordinate[j], 
        width=data_interval_ms * 0.9,  # 90% of interval to avoid overlap
        height=1, 
        line_color=None, 
        line_alpha=0, 
        fill_color=transform(name, mapper), 
        dilate=False,  # Try disabling this first
        source=source_dic
    )

2. Diagnose the Shared x_range Impact

You mentioned suspecting the x_range=p1.x_range parameter—this is a good lead! If p1 has a very wide x-axis range (e.g., spanning days/weeks), your small rects will look tiny and spaced out relative to the full axis.

Test this quickly by temporarily removing x_range=p1.x_range from plot_options. If the rects display normally without gaps, the shared range is the issue. To fix this while keeping interactive linking:

  • Use a RangeTool to let users zoom into the relevant time range on p4, while still linking to p1.
  • Or explicitly set p4.x_range.start and p4.x_range.end to focus on your anomaly data’s time window.

3. Compare to Your Working Third Plot

Since your third plot displays narrow rects correctly, compare its code to p4’s:

  • Is its width set to a value matching the data interval?
  • Does it use the same source data or a different one?
  • Is dilate=True enabled there too? (Dilate expands rects to fit tick intervals, which can create gaps if ticks are sparse.)

4. Verify Your source_dic Data

Double-check that the estampilles field in source_dic is formatted correctly:

  • It should be a datetime type (Python datetime objects, NumPy datetime64, or epoch milliseconds). String timestamps can cause unexpected spacing.
  • Ensure there are no missing or duplicate timestamps that would create artificial gaps between rects.

Give these steps a try—chances are adjusting the width to match your data’s time interval or tweaking the shared x-range will resolve those big gaps.

内容的提问来源于stack exchange,提问作者GuiCharles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 06:52:32