简体   繁体   中英

Bokeh - how to add y value to y-axis of horizontal Span?

How can I show and highlight the value of the horizontal Span on the y-axis?

import bokeh as bk
import bokeh.plotting as bkplot
#bkplot.output_notebook() # to show inline

x = np.arange(3)
y = x**2

source = bk.models.ColumnDataSource(dict(x=x, y=y))

p = bkplot.figure(plot_width=500, plot_height=300)

glyph = bk.models.Line(x="x", y="y", line_width=6)
p.add_glyph(source, glyph)

top_span = bk.models.Span(location=3.5, dimension='width', line_color='green', line_width=2)
bottom_span = bk.models.Span(location=1.5, dimension='width', line_color='red', line_width=2)
p.renderers.extend([top_span, bottom_span])

bkplot.show(p)

Result:

在此处输入图片说明

Desired result:

在此处输入图片说明

There's been a discussion of this very issue in Bokeh's github: https://github.com/bokeh/bokeh/issues/7309

So having labels on Spans in the axis area vs. the plot area is not yet implemented.

If you're okay with labels in the plot area, though, you may want to use a Label. Here's what I was able to do with Labels:

top_span = bk.models.Span(location=3.5, dimension='width', line_color='green', line_width=2)
top_span_label = bk.models.Label(text_color='green', text=str(top_span.location), x=0, y=top_span.location)
bottom_span = bk.models.Span(location=1.5, dimension='width', line_color='red', line_width=2)
bottom_span_label = bk.models.Label(text_color=bottom_span.line_color, text=str(bottom_span.location), x=0, y=bottom_span.location)
p.renderers.extend([top_span, top_span_label, bottom_span, bottom_span_label])

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM