Bokeh矩形Glyph使用问题:宽度无效与X轴共享导致间隙
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
RangeToolto let users zoom into the relevant time range onp4, while still linking top1. - Or explicitly set
p4.x_range.startandp4.x_range.endto 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
widthset to a value matching the data interval? - Does it use the same
sourcedata or a different one? - Is
dilate=Trueenabled 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
datetimeobjects, NumPydatetime64, 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

