Bokeh corrects plot with dates on the x axis shifts checkmarks to the right

I am trying to adapt the brewer's example ( http://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html ) to my needs. One of the things I would like is to have dates on the x axis. I have done the following:

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

as an adaptation of the previous line

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

Dates are displayed, but the first date 1950-01-01 has a value of x = 1. How can I shift it to x = 0? The first real data that I have is related to this date and therefore should be displayed along with this date, and not in a month.

Graphical explanation

+4
source share
1 answer

, x, , , 1, x, 1. (http://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html) 0 19, 20 , 19, timesteps. 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)

:

enter image description here

UPDATE

data['x'] = np.arange(0,N) 0 19, offset=-1 FactorRange, .. 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)
+1

Source: https://habr.com/ru/post/1670674/


All Articles