gpt4 book ai didi

python - 如何获取 HoverTool 上的轴值 - Bokeh

转载 作者:行者123 更新时间:2023-12-01 08:25:05 26 4
gpt4 key购买 nike

我编写了一个从 .csv 文件读取并将其转换为 pandas 数据帧的代码。然后我继续用烛台绘制图表。图表本身很好,但是当我尝试使用 HoverTools 时,我似乎无法将轴值添加到工具中。我使用了列数据源,但我无法理解它。

  import pandas as pd

from math import pi

from bokeh.plotting import figure, show, output_file

from bokeh.models import HoverTool

df = pd.read_csv('/Users/robindhillon/Desktop/pyfiles/EURUSD.csv')

df.columns = ['date','open','high','low','close','volume']

df['date'] = pd.to_datetime([x[:-9] for x in
df['date'].squeeze().tolist()], dayfirst=True)

inc = df.close > df.open
dec = df.open > df.close
w = 86400000


hover = HoverTool(
tooltips=[
('date', '@date'),
('open', '@open' ),
('high', '@high' ),
('low', '@low' ),
('close', '@close'),
],

formatters={
'date' : 'datetime',
'open' : 'numeral',
'high' : 'numeral',
'low' : 'numeral',
'close': 'numeral',
},

mode='vline'
)

TOOLS = 'pan,wheel_zoom,box_zoom,reset,save,crosshair'

p = figure(x_axis_type = 'datetime', tools=TOOLS, plot_width=1200,
title='test')

p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha = 0.3

p.add_tools(hover)

p.segment(df.date, df.high, df.date, df.low, color="black")

p.vbar(df.date[inc], w, df.open[inc], df.close[inc],
fill_color="#12C98C", line_color="black")

p.vbar(df.date[dec], w, df.open[dec], df.close[dec],
fill_color="#F2583E", line_color="black")

output_file("candlestick.html", title="candlestick.py example")

show(p) # open a browser

最佳答案

以@开头的字段名称与ColumnDataSource中的列相关联。例如,每当触发悬停时,字段名称“@date”将显示“date”列中的值。如果悬停针对第 17 个字形,则悬停工具提示将相应地显示第 17 个日期值。如果您使用以 @ 开头且没有 ColumnDataSource

的字段名称,悬停工具将无法工作
import pandas as pd
from math import pi
from bokeh.plotting import figure, show, output_file
from bokeh.models import HoverTool, ColumnDataSource

df = pd.read_csv('/Users/robindhillon/Desktop/pyfiles/EURUSD.csv')
df.columns = ['date','open','high','low','close','volume']
df['date'] = pd.to_datetime([x[:-9] for x in
df['date'].squeeze().tolist()], dayfirst=True)

inc = df.close > df.open
dec = df.open > df.close
w = 86400000

hover = HoverTool(
tooltips=[
('date', '@date{%F}'),
('open', '@open' ),
('high', '@high' ),
('low', '@low' ),
('close', '@close'),
],
formatters={
'date' : 'datetime',
'open' : 'numeral',
'high' : 'numeral',
'low' : 'numeral',
'close': 'numeral',
},
mode='vline'
)

df['dateinc'] = df.date[inc]
df['openinc'] = df.open[inc]
df['closeinc'] = df.close[inc]
df['datedec'] = df.date[dec]
df['opendec'] = df.open[dec]
df['closedec'] = df.close[dec]

source = ColumnDataSource(df)

TOOLS = 'pan,wheel_zoom,box_zoom,reset,save,crosshair'

p = figure(x_axis_type = 'datetime', tools=TOOLS, plot_width=1200,
title='test')

p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha = 0.3

p.add_tools(hover)
p.segment('date', 'high', 'date', 'low', color="black", source=source)
p.vbar('dateinc', w, 'openinc', 'closeinc', fill_color="#12C98C", line_color="black", source=source)

p.vbar('datedec', w, 'opendec', 'closedec', fill_color="#F2583E", line_color="black", source=source)

output_file("candlestick.html", title="candlestick.py example")

show(p) # open a browser

关于python - 如何获取 HoverTool 上的轴值 - Bokeh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316623/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com