代码之家  ›  专栏  ›  技术社区  ›  user2661518

brewer.py将x轴转换为日期

  •  1
  • user2661518  · 技术社区  · 7 年前

    为了理解bokeh的概念,我试图将这个示例改为x轴,并加上日期。 https://bokeh.pydata.org/en/latest/docs/gallery/brewer.html

    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']
    
    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((df.index[::-1], df.index))
    p = figure(x_range=(0, N-1), y_range=(0, 50))
    # p.xaxis.major_label_overrides = {
    #     i: date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(dates))
    # }
    # p.xaxis.bounds = (0, df.index[-1])
    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)
    
    output_file('brewer.html', title='brewer.py example')
    
    show(p)
    

    通过上面的例子我得到了x轴 0,1,2,3 我可以放大。

    我该怎么把这个改到现在。我能做点什么吗 x_range=dates ,只需遍历日期列表。 我试过使用带注释的代码,但它不更新次要标签,我无法放大。 日期必须在 df'? Currently my DF’IS

       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轴上绘制这些日期。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Pablo Reyes    7 年前

    你可以使用 x_axis_type ='日期时间' bokeh.plotting.figure 函数指示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)
    

    enter image description here