Python brewer.py将x轴转换为日期
我试图将此示例更改为带有日期的x轴,以理解bokeh概念 通过上面的例子,我得到了x轴Python brewer.py将x轴转换为日期,python,pandas,datetime,dataframe,bokeh,Python,Pandas,Datetime,Dataframe,Bokeh,我试图将此示例更改为带有日期的x轴,以理解bokeh概念 通过上面的例子,我得到了x轴0,1,2,3,我可以放大 如何将此更改为最新版本。我可以做一些类似于x\u range=dates的事情,只需遍历日期列表即可。 我尝试使用注释代码,但它不会更新次要标签,也无法放大。 日期是否必须在df'中? 目前我的df` y0 y1 y2 0 2 3 4 1 2 2 3 2 0 0 0 3 1 2 3 如果dates必须在df中,我可以添加如
0,1,2,3
,我可以放大
如何将此更改为最新版本。我可以做一些类似于x\u range=dates
的事情,只需遍历日期列表即可。
我尝试使用注释代码,但它不会更新次要标签,也无法放大。
日期是否必须在df'中?
目前我的
df`
y0 y1 y2
0 2 3 4
1 2 2 3
2 0 0 0
3 1 2 3
如果dates
必须在df
中,我可以添加如下内容
y0 y1 y2 date
0 2 3 4 2016-06-01
1 2 2 3 2016-06-02
2 0 0 0 2016-06-03
3 1 2 3 2016-06-04
但仍然不确定如何在x轴上绘制这些日期。您可以在
bokeh.plotting.figure中使用x轴类型
='datetime',以指示x轴将显示时间
import pandas as pd
import numpy as np
import bokeh
import bokeh.plotting
N = 4
cats = 3
data = [[2,3,4], [2,2,3], [0,0,0], [1,2,3]]
df = pd.DataFrame(data, columns=['y0', 'y1', 'y2'])
print df
dates = ['2016-06-01','2016-06-02','2016-06-03','2016-06-04']
dt = [pd.datetime.strptime(x,'%Y-%m-%d') for x in dates]
def stacked(df):
df_top = df.cumsum(axis=1)
df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
return df_stack
areas = stacked(df)
colors = bokeh.palettes.brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((dt[::-1], dt))
p = bokeh.plotting.figure(x_axis_type='datetime', y_range=(0, 10))
p.xaxis.formatter = bokeh.models.formatters.DatetimeTickFormatter(
days=["%Y-%m-%d"])
p.grid.minor_grid_line_color = '#eeeeee'
p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
color=colors, alpha=0.8, line_color=None)
bokeh.io.output_file('brewer.html', title='brewer.py example')
bokeh.io.show(p)
import pandas as pd
import numpy as np
import bokeh
import bokeh.plotting
N = 4
cats = 3
data = [[2,3,4], [2,2,3], [0,0,0], [1,2,3]]
df = pd.DataFrame(data, columns=['y0', 'y1', 'y2'])
print df
dates = ['2016-06-01','2016-06-02','2016-06-03','2016-06-04']
dt = [pd.datetime.strptime(x,'%Y-%m-%d') for x in dates]
def stacked(df):
df_top = df.cumsum(axis=1)
df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
return df_stack
areas = stacked(df)
colors = bokeh.palettes.brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((dt[::-1], dt))
p = bokeh.plotting.figure(x_axis_type='datetime', y_range=(0, 10))
p.xaxis.formatter = bokeh.models.formatters.DatetimeTickFormatter(
days=["%Y-%m-%d"])
p.grid.minor_grid_line_color = '#eeeeee'
p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
color=colors, alpha=0.8, line_color=None)
bokeh.io.output_file('brewer.html', title='brewer.py example')
bokeh.io.show(p)