简体   繁体   English

如何将 x 轴设置为散景图上的日期时间?

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

I'm using bokeh with an ipython notebook.我在 ipython 笔记本上使用散景。

I want to plot a line graph in bokeh using a pandas DataFrame containing datetimes:我想使用包含日期时间的 Pandas DataFrame 在散景中绘制折线图:

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. FWIW,自问题首次发布以来,默认行为已更改。 The original code now yields:原始代码现在产生:

代码的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 x 轴上绘制带有日期时间的线性回归 - How to plot a linear regression with datetimes on the x-axis 如何使用 matplotlib 中的日期时间更改 x 轴的范围? - How do I change the range of the x-axis with datetimes in matplotlib? 如何为从pandas DataFrame创建的条形图设置x轴刻度位置? - How can I set the x-axis tick locations for a bar plot created from a pandas DataFrame? 我如何在 Bokeh 中两次放置相同的 x 轴 - How I put same x-axis twice in Bokeh 在图上显示所有x轴值-散景 - Show all x-axis values on plot - Bokeh 如何在 python 中以像素数为 x 轴,灰度颜色为 y 轴 plot 图形? - How can I plot the figure with the number of pixel as a x-axis and the grayscale color as a y-axis in python? 如何使plot的x轴原点和y轴原点在matplotlib中重叠? - How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib? 如何将带有偏移量的箭头注释添加到带有日期时间 x 轴的散景 plot - How to add Arrow annotations with an offset to a bokeh plot with a datetime x-axis 如何在 Python 中将分钟时间设置为 Matplotlib plot 的 x 轴? - How to set minutes time as x-axis of a Matplotlib plot in Python? 如何指定要在x轴上绘制的离散值(matplotlib,boxplot)? - How can I specify the discrete values that I want to plot on the x-axis (matplotlib, boxplot)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM