放大时的动态索引 Python 散景

Dynamic indexes while zooming in Python Bokeh

我对 Bokeh 还很陌生,并尝试实现以下目标:

我有一个数据集,其中包含日期格式为 dd-mm-yyyy 的行。 计算日期然后绘制。 放大时,我希望 Bokeh 显示个人日期(已经有效)。 缩小时,我希望 Bokeh 仅显示月份(或进一步缩小时的年份)。没错,由于缩小的次数越多,各个日期越接近,索引变得非常混乱。

有没有办法让 Bokeh 根据放大或缩小的距离更改索引中显示的内容?

这是我的代码:

import pandas as pd
from bokeh.charts import TimeSeries
from bokeh.io import output_file, show, gridplot

transactionssent = dict(pd.melt(df,value_vars=['datesent']).groupby('value').size())
transactionssent2 = pd.DataFrame.from_dict(transactionssent, orient= 'index')
transactionssent2.columns = ['Amount']
transactionssent2.index.rename('Date sent', inplace= True)

ts = TimeSeries(transactionssent2, x='index', y='Amount')
ts.xaxis.axis_label = 'Date sent'

如果有人知道请指出正确的方向。

谢谢并致以最诚挚的问候, 斯特凡

你所描述的你想要的已经听起来像是内置日期时间轴的标准行为。所以,我的猜测是 TimeSeries 将您的日期视为 string/categorical 值,这可以解释为什么您没有看到标准的日期时间轴缩放。

我应该补充一点,bokeh.charts(包括 TimeSeries)最近已被移至一个单独的项目,并且已知存在问题。我实际上不鼓励在这一点上使用它。幸运的是,使用 bokeh.plotting API 绘制时间序列也很容易,它很稳定,经过充分测试和记录,并且被广泛使用。

这里有一个例子来演示:

import datetime
import numpy as np

from bokeh.io import show, output_file
from bokeh.plotting import figure

# some fake data just for this example, Pandas columns work fine too
start = datetime.datetime(2017, 1, 1)
x = np.array([start + datetime.timedelta(hours=i) for i in range(800)])
y = np.sin(np.linspace(0, 2, len(x))) + 0.05 * np.random.random(len(x))

p = figure(x_axis_type="datetime")
p.line(x, y)

output_file("stocks.html")

show(p)

第一次显示的轴是这样的:

但是放大后像这样:


您还可以通过在 p.xaxis[0].formatter 上设置各种属性来进一步自定义日期的格式化方式。有关可用属性的详细信息,请参阅参考指南:

http://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter