散景中的TimeSeries使用带有索引的数据框 [英] TimeSeries in Bokeh using a dataframe with index
问题描述
我正在尝试使用Bokeh绘制一个Pandas
数据帧,其中的DateTime
列包含年份和一个数字.如果将DateTime
指定为x
,则行为是预期的(x轴上的年).但是,如果使用set_index
将DateTime
列转换为数据帧的索引,然后仅在TimeSeries
中指定y
,则我在x轴上得到的时间以毫秒为单位.一个最小的例子
I'm trying to use Bokeh to plot a Pandas
dataframe with a DateTime
column containing years and a numeric one. If the DateTime
is specified as x
, the behaviour is the expected (years in the x-axis). However, if I use set_index
to turn the DateTime
column into the index of the dataframe and then only specify the y
in the TimeSeries
I get time in milliseconds in the x-axis. A minimal example
import pandas as pd
import numpy as np
from bokeh.charts import TimeSeries, output_file, show
output_file('fig.html')
test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})
fig = TimeSeries(test,x='datetime',y='foo')
show(fig)
output_file('fig2.html')
test = test.set_index('datetime')
fig2 = TimeSeries(test,y='foo')
show(fig2)
这是预期的行为还是错误?我希望两种方法都可以看到相同的画面.
Is this the expected behaviour or a bug? I would expect the same picture with both approaches.
干杯!
推荐答案
Bokeh出于内部原因而添加了索引,但是从最近的版本(> = 0.12.x)开始,它不再这样做.同样值得注意的是,bokeh.charts
API已被弃用和删除.使用稳定的bokeh.plotting
API的等效代码会产生预期的结果:
Bokeh used to add an index for internal reasons but as of not-so-recent versions (>= 0.12.x) it no longer does this. Also it's worth noting that the bokeh.charts
API has been deprecated and removed. The equivalent code using the stable bokeh.plotting
API yields the expected result:
import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import row
output_file('fig.html')
test = pd.DataFrame({'datetime':pd.date_range('1/1/1880', periods=2000),'foo':np.arange(2000)})
fig = figure(x_axis_type="datetime")
fig.line(x='datetime',y='foo', source=test)
test = test.set_index('datetime')
fig2 = figure(x_axis_type="datetime")
fig2.line(x='datetime', y='foo', source=test)
show(row(fig, fig2))
这篇关于散景中的TimeSeries使用带有索引的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!