我正在尝试在 Bokeh 内的条形图顶部绘制一条线。我尝试过:
p1 = figure()...
p1.renderer.append(Bar(...))
p1.renderer.append(Line(...))
show(p1)
到目前为止我还没有运气。
最佳答案
使用 Basic Glyphs 可以在 Bokeh 中的一个图中组合两个或多个图形。 。
对于您的问题,我们可以使用直线和矩形。
from bokeh.plotting import figure, output_file, show
from bokeh.models.ranges import Range1d
import numpy
output_file("line_bar.html")
p = figure(plot_width=400, plot_height=400)
# add a line renderer
p.line([1, 2, 3, 4, 5], [6, 7, 6, 4, 5], line_width=2)
# setting bar values
h = numpy.array([2, 8, 5, 10, 7])
# Correcting the bottom position of the bars to be on the 0 line.
adj_h = h/2
# add bar renderer
p.rect(x=[1, 2, 3, 4, 5], y=adj_h, width=0.4, height=h, color="#CAB2D6")
# Setting the y axis range
p.y_range = Range1d(0, 12)
p.title = "Line and Bar"
show(p)
我们得到的情节:
关于Bokeh,条形图和折线图的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30643681/