python – 带有日期的散景补丁图,x轴将刻度向右移动

我正在努力使酿酒商的例子(
http://bokeh.pydata.org/en/latest/docs/gallery/brewer.html)适应我的需要.我想要的一件事就是在x轴上设置日期.我做了以下事情:

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://bokeh.pydata.org/en/latest/docs/gallery/brewer.html)的范围是0到19,所以它有20个数据点而不是19个像你的时间步长列表.我将绘图的x输入修改为:data [‘x’] = np.arange(1,N 1)从1开始到N.我又在你的列表中添加了一天:timesteps = [str(x. pddate_range中的x的日期())(‘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)

这是输出:

《python – 带有日期的散景补丁图,x轴将刻度向右移动》

UPDATE

你可以将数据[‘x’] = np.arange(0,N)从0保留到19,然后在FactorRange中使用offset = -1,即figure(x_range = bokeh.models.FactorRange(factors = timesteps,offset = -1),…

更新版本散景0.12.16

在这个版本中,我使用日期时间为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)
点赞