本文介绍了放大Python Bokeh时的动态索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am fairly new to Bokeh and try to achieve the following:

我有一个数据集,其中的行包含日期,格式为dd-mm-yyyy.计算日期,然后绘制.当放大时,我希望Bokeh显示单个日期(已经有效).缩小时,我希望Bokeh仅显示月份(或缩小时显示年).正确地知道,由于各个日期越远,缩小的索引就越乱.

I have a dataset with rows containing dates in the format dd-mm-yyyy.The dates are counted and then plotted.When zoomed in I want Bokeh to show the indiviudal dates (that works already).When zoomed out I want Bokeh only to show the months (or years when zoomed out even further). Right know the index gets pretty messy due to individual dates getting closer and closer the more you zoom out.

是否有一种方法可以告诉Bokeh根据您放大或缩小的程度来更改索引中显示的内容?

Is there a way to tell Bokeh to change what is shown in the index depending on how far you zoomed in or out?

这是我的代码:

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'

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

If someone knows please point me in the right direction.

感谢和最诚挚的问候,斯蒂芬

Thanks and best regards,Stefan

推荐答案

您所描述的内容听起来像内置日期时间轴的标准行为.因此,我的猜测是TimeSeries将您的日期视为字符串/分类值,这可以解释为什么您没有看到标准的日期时间轴缩放.

What you've described as what you want already sounds like the standard behavior of the built in datetime axis. So, my guess is that TimeSeries is treating your dates as string/categorical values, which would explain why you are not seeing standard datetime axis scaling.

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

I should add that bokeh.charts (including TimeSeries) has recently been removed to a separate project and also is known to have problems. I would actually discourage it's use at this point. Fortunately, it's also easy to plot timeseries with the bokeh.plotting API, which is stable, well-tested and documented, and in widespread use.

以下是一个演示示例:

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)

第一次显示时,其轴是这样的:

Whose axis looks like this when first displayed:

但是像这样放大时:

But like this when zoomed in:

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

You can also further customize how the dates are formatter by setting various properties on the p.xaxis[0].formatter. For details about available properties, see the reference guide:

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

这篇关于放大Python Bokeh时的动态索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:47