为什么我的hovertool(来自bokeh)显示的结果不正确?

2024-04-19 16:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在bokeh中的悬停工具在图形上悬停时显示不正确的结果。以下是我的代码的一部分,其中包括我定义悬停工具的地方:

headers = ['ticker', 'size', 'price', 'unix','bid','date']
dtypes = {'ticker': 'str', 'size': 'float', 'price': 'float', 'unix': 'float','ask': 'str','date': 'str'}
parse_dates = ['date']
btcnow = pd.read_csv('new 1031-113.csv', header=None, names=headers, dtype=dtypes, parse_dates=parse_dates)
#btc105=pd.read_excel('11-3-11-5 data.xlsx',sheet_name="Sheet1",header=None)
#btc103=btc103.append(btc105,ignore_index = True)
now3 = pd.DataFrame(btcnow, columns=['size','date'])
now4 = pd.DataFrame(btcnow, columns=['date','price','size'])
x1 = now3.loc[now3["size"] == 31, "date"]
y1 = now3.loc[now3["size"] == 31, "size"]
z1= now4.loc[now4["size"] == 31, "date"]
w1=now4.loc[now4["size"] == 31, "price"]
hover = HoverTool(tooltips=[('index', "$index"),
    ("Date", "@date_time{%Y-%m-%d %H:%M:%S.%f}"),
   ("(x1,y1)", "(@x1, @y1)"),
("(z1,w1)", "(@z1, @w1)"),
])

我希望能够将鼠标悬停在数据上并查看日期和时间,但它显示的是unix时间,我该如何修复它? 下面是我的数据片段:data


Tags: sizedateindexparseunixfloatpriceloc
1条回答
网友
1楼 · 发布于 2024-04-19 16:21:36

它不是向您显示不正确的结果,而是向您显示正确的结果,其格式与您想要的不同。设置悬停工具提示的格式为covered in the documentation。您需要通过配置悬停工具的formatters属性,告诉Bokeh您想要为任何想要使用日期时间格式化程序的字段使用日期时间格式化程序:

formatters={
    '@date_time' : 'datetime', # use 'datetime' formatter for 'date_time' field
}

相关问题 更多 >