好吧,如果你有一个字符串作为你的x轴的列表,那么显然是从1开始计数,那么你必须修改你的X数据的情节可处第1其实,啤酒开始示例(http://bokeh.pydata.org/en/latest/docs/gallery/brewer.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)
这里是输出:
UPDATE
您可以从0离开data['x'] = np.arange(0,N)
至19,然后用offset=-1
内FactorRange
,即figure(x_range=bokeh.models.FactorRange(factors=timesteps,offset=-1),...
不熟悉'bokeh'。但为什么将日期时间转换为字符串? – Parfait
好问题。可能不是最小工作示例的一部分,但是在代码后面的某些内容中,我没有包括它的必要性。 –