x-axis 将刻度向右移动一位时,带有日期的 Bokeh 补丁图

Bokeh patches plot with dates as x-axis shifts the ticks one to the right

我正在尝试根据我的需要调整 brewer 示例 (http://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html)。我想要的一件事是在 x-axis 有约会。我做了以下事情:

timesteps = [str(x.date()) for x in pd.date_range('1950-01-01', '1951-07-01', freq='MS')]
p = figure(x_range=FactorRange(factors=timesteps), y_range=(0, 800))
p.xaxis.major_label_orientation = np.pi/4

作为上一行的改编

p = figure(x_range=(0, 19), y_range=(0, 800))

显示日期,但第一个日期 1950-01-01 位于 x=1。我怎样才能将它移动到 x=0?我拥有的第一个真实数据点是针对该日期的,因此应该与该日期一起显示,而不是在一个月后显示。

好吧,如果你有一个字符串列表作为你的 x 轴,那么显然计数从 1 开始,那么你必须修改你的 x 数据以使绘图从 1 开始。实际上是 brewer 示例(http://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html) 的范围从 0 到 19,因此它有 20 个数据点,而不是像 timesteps 列表那样的 19 个。我将绘图的 x 输入修改为:data['x'] = np.arange(1,N+1) 从 1 到 N。我又在你的列表中添加了一天:timesteps = [str(x.date()) for x in pd.date_range('1950-01-01', '1951-08-01', freq='MS')] 这是完整的代码:

import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

N = 20
categories = ['y' + str(x) for x in range(10)]
data = {}
data['x'] = np.arange(1,N+1)
for cat in categories:
    data[cat] = np.random.randint(10, 100, size=N)

df = pd.DataFrame(data)
df = df.set_index(['x'])

def stacked(df, categories):
    areas = dict()
    last = np.zeros(len(df[categories[0]]))
    for cat in categories:
        next = last + df[cat]
        areas[cat] = np.hstack((last[::-1], next))
        last = next
    return areas

areas = stacked(df, categories)

colors = brewer["Spectral"][len(areas)]

x2 = np.hstack((data['x'][::-1], data['x']))


timesteps = [str(x.date()) for x in pd.date_range('1950-01-01', '1951-08-01', freq='MS')]
p = figure(x_range=bokeh.models.FactorRange(factors=timesteps), y_range=(0, 800))

p.grid.minor_grid_line_color = '#eeeeee'

p.patches([x2] * len(areas), [areas[cat] for cat in categories],
          color=colors, alpha=0.8, line_color=None)
p.xaxis.major_label_orientation = np.pi/4
bokeh.io.show(p)

这是输出:

更新

你可以把data['x'] = np.arange(0,N)从0留到19,然后在FactorRange里面用offset=-1,即figure(x_range=bokeh.models.FactorRange(factors=timesteps,offset=-1),...

更新版本 bokeh 0.12.16

在这个版本中,我使用 datetime 作为 x 轴,这样在放大时具有更好的格式优势。

import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

timesteps = [x for x in pd.date_range('1950-01-01', '1951-07-01', freq='MS')]
N = len(timesteps)
cats = 10

df = pd.DataFrame(np.random.randint(10, 100, size=(N, cats))).add_prefix('y')

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 = brewer['Spectral'][areas.shape[1]]


x2 = np.hstack((timesteps[::-1], timesteps))

p = figure( x_axis_type='datetime', y_range=(0, 800))
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)
p.xaxis.formatter = bokeh.models.formatters.DatetimeTickFormatter(
    months=["%Y-%m-%d"])
p.xaxis.major_label_orientation = 3.4142/4
output_file('brewer.html', title='brewer.py example')

show(p)