散景垂直栏位置与股票代码不对齐

问题描述 投票:1回答:1

这是一个绘制一些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后,垂直条与股票代码位置不对齐: -

enter image description here

在没有x_range && y_range的正常情况下,它工作正常: -

enter image description here

我想知道控制vbar位置的参数是什么?收到自定义股票代码后,为什么他们“感动”?

python bokeh
1个回答
1
投票

由于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)
© www.soinside.com 2019 - 2024. All rights reserved.