这是一个绘制一些vBars(jupyter笔记本)的片段:
import random
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool, FactorRange, Range1d
from bokeh.models.glyphs import VBar
from bokeh.plotting import figure
from bokeh.io import show, output_notebook
# data
data = {'x': [], 'y': [], 'z': []}
for i in range(1, 10+1):
data['x'].append(i)
data['y'].append(random.randint(1, 100))
data['z'].append(random.uniform(1.00, 1000.00))
source = ColumnDataSource(data)
xdr = FactorRange(factors=[str(x) for x in data['x']])
ydr = Range1d(start=0, end=max(data['y'])*1.5)
f = figure(x_range=xdr, y_range=ydr, plot_width=1000, plot_height=300, tools='',
toolbar_location='above', title='title', outline_line_color='gray')
glyph = VBar(x='x', top='y', bottom=0,
width=0.8, fill_color='blue')
f.add_glyph(source, glyph)
f.add_tools(HoverTool(
tooltips=[
('time', '$x{0}'),
('value', '@' + 'y' + '{0}'),
('money', '@z')
],
mode='vline'
))
output_notebook()
show(f)
通过x_range
&& y_range
后,垂直条与股票代码位置不对齐: -
在没有x_range
&& y_range
的正常情况下,它工作正常: -
我想知道控制vbar位置的参数是什么?收到自定义股票代码后,为什么他们“感动”?
由于FactorRange,它未对齐。不确定为什么......我用ColumnDataSource的min和max值替换了它,这很好用。
import random
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, HoverTool, FactorRange, Range1d
from bokeh.models.glyphs import VBar
from bokeh.plotting import figure
from bokeh.io import show
# data
data = {'x': [], 'y': [], 'z': []}
for i in range(1, 10+1):
data['x'].append(i)
data['y'].append(random.randint(1, 100))
data['z'].append(random.uniform(1.00, 1000.00))
source = ColumnDataSource(data)
ydr = Range1d(start=0, end=max(data['y'])*1.5)
f = figure(x_range=(min(source.data['x'])-0.5, max(source.data['x'])+0.5), y_range=ydr, plot_width=1000, plot_height=300, tools='', toolbar_location='above', title='title', outline_line_color='gray')
glyph = VBar(x='x', top='y', bottom=0,
width=0.8, fill_color='blue')
f.add_glyph(source, glyph)
f.add_tools(HoverTool(
tooltips=[
('time', '$x{0}'),
('value', '@' + 'y' + '{0}'),
('money', '@z')
],
mode='vline'
))
show(f)