简体   繁体   中英

How can I set the x-axis as datetimes on a bokeh plot?

I'm using bokeh with an ipython notebook.

I want to plot a line graph in bokeh using a pandas DataFrame containing datetimes:

import pandas as pd
from datetime import datetime as dt
from bokeh.io import output_notebook
from bokeh.charts import Bar, Line, show

df = pd.DataFrame(data=[1,2,3],
                  index=[dt(2015, 1, 1), dt(2015, 1, 2), dt(2015, 1, 3)],
                  columns=['foo'])

output_notebook()
show(Line(df))

However, bokeh uses microseconds! Why is this? How do I fix it?

线的散景图

从散景 0.12.3 开始,您现在可以执行以下操作:

p = figure(..., x_axis_type='datetime', ...)

is that ok ?

在此处输入图片说明

import pandas as pd
from math import pi
from datetime import datetime as dt
from bokeh.io import output_file
from bokeh.charts import show
from bokeh.models import DatetimeTickFormatter
from bokeh.plotting import figure

df = pd.DataFrame(data=[1,2,3],
                  index=[dt(2015, 1, 1), dt(2015, 1, 2), dt(2015, 1, 3)],
                  columns=['foo'])
p = figure(plot_width=400, plot_height=400)
p.line(df.index, df['foo'])
p.xaxis.formatter=DatetimeTickFormatter(
        hours=["%d %B %Y"],
        days=["%d %B %Y"],
        months=["%d %B %Y"],
        years=["%d %B %Y"],
    )
p.xaxis.major_label_orientation = pi/4
output_file('myplot.html')
show(p)

FWIW, the default behavior has changed since the question was first posted. The original code now yields:

代码的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM