Dynamic indexes while zooming in Python Bokeh

北城以北 提交于 2020-02-08 07:31:26

问题


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

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.

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

Here is my code:

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


回答1:


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.

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.

Here is an example to demonstrate:

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:


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



来源:https://stackoverflow.com/questions/44606211/dynamic-indexes-while-zooming-in-python-bokeh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!