以下是绘制一些vBar(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位置的参数是什么?为什么他们在收到自定义股票名称后就“动了”?
答案 0 :(得分:1)
由于FactorRange,它未对齐。不太清楚为什么...我用ColumnDataSource的最小值和最大值代替了它,并且效果很好。
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)