问题
I am plotting some data on x-axis and y-axis which gives peaks. I want to highlight certain areas of the peaks in multiple colors.
I was able to get the plot but have no clue on how to add overlays to the plot
https://jascoinc.com/wp-content/uploads/2013/12/UV-fraction-collection.png
回答1:
You can highlight an area by plotting another glyph over the line such as a vbar, but other glyphs like rectangles also work.
from bokeh.plotting import figure, show
p = figure(plot_width=400, plot_height=400)
p.line([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 3, 2, 8, 2, 1, 2, 3, 1, 9, 1, 2], line_width=2)
p.vbar(x=[4, 10], width=2, bottom=0, top=10, color=['red', 'green'], alpha=0.5)
show(p)
回答2:
You could use BoxAnnotation
for this. One advantage is that it always span entire height of the plot.
from bokeh.plotting import figure, show
from bokeh.models import BoxAnnotation
p = figure(plot_width=400, plot_height=400)
p.line([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 3, 2, 8, 2, 1, 2, 3, 1, 9, 1, 2], line_width=2)
b1 = BoxAnnotation(left=3, right = 5, fill_color = 'red', fill_alpha = 0.5)
b2 = BoxAnnotation(left=9, right = 11, fill_color = 'green', fill_alpha = 0.5)
p.add_layout(b1)
p.add_layout(b2)
p.text([4, 10], [0, 0], ['area1', 'area2'], y_offset = -10, x_offset = -20)
show(p)
来源:https://stackoverflow.com/questions/56248284/is-there-any-way-to-add-overlays-to-bokeh-plot-highlighting-certain-areas-based