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

后端 未结 3 1281
遇见更好的自我
遇见更好的自我 2020-12-08 13:29

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
fr         


        
3条回答
  •  隐瞒了意图╮
    2020-12-08 14:05

    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)
    

提交回复
热议问题