Search code examples
pythondata-visualizationbokeh

Display only part of Y-axis on Bokeh


Using Bokeh 0.8.1, how can i display a long timeserie, but start 'zoomed-in' on one part, while keeping the rest of data available for scrolling ?

For instance, considering the following time serie (IBM stock price since 1980), how could i get my chart to initially display only price since 01/01/2014 ?

Example code :

import pandas as pd
import bokeh.plotting as bk
from bokeh.models import ColumnDataSource
bk.output_notebook()
TOOLS="pan,wheel_zoom,box_zoom,reset,save"

# Quandl data, too lazy to generate some random data
df = pd.read_csv('https://www.quandl.com/api/v1/datasets/GOOG/NYSE_IBM.csv')
df['Date'] = pd.to_datetime(df['Date'])
df = df[['Date', 'Close']]

#Generating a bokeh source
source = ColumnDataSource()
dtest = {}
for col in df:
    dtest[col] = df[col]

source = ColumnDataSource(data=dtest)

# plotting stuff !
p = bk.figure(title='title', tools=TOOLS,x_axis_type="datetime", plot_width=600, plot_height=300)
p.line(y='Close', x='Date', source=source)
bk.show(p)

outputs :

IBM

but i want to get this (which you can achieve with the box-zoom tool - but I'd like to immediately start like this)

IBM


Solution

  • Edit: the answer below is very outdated. Most common datetime types have been usable directly with Bokeh ranges for may years now.


    OLD ANSWER:

    So, it looks (as of 0.8.1) that we need to add some more convenient ways to set ranges with datetime values. That said, although this is a bit ugly, it does currently work for me:

    import time, datetime
    x_range = (
        time.mktime(datetime.datetime(2014, 1, 1).timetuple())*1000,
        time.mktime(datetime.datetime(2016, 1, 1).timetuple())*1000
    )
    p = bk.figure(
        title='title', tools=TOOLS,x_axis_type="datetime",     
        plot_width=600, plot_height=300, x_range=x_range
    )