Web Plotting using Bokeh in Python 101




webIntegration








In [1]:
#################################################
#       Web Plotting using Bokeh in Python      #
# In this tutorial, we will cover Bokeh usage   #
# 1. static plots                               #
# 2. interactive plots                          #
#################################################   


#################################################
#               1. Static Plots                 #
#################################################
import numpy as np
import pandas as pd
%matplotlib inline

url = 'http://ichart.yahoo.com/table.csv?s=MSFT&a=0&b=1&c=2009'
data = pd.read_csv(url, parse_dates=['Date'])
data.head()
Out[1]:
Date Open High Low Close Volume Adj Close
0 2016-06-21 50.200001 51.430000 50.160000 51.189999 33989400 51.189999
1 2016-06-20 50.639999 50.830002 50.029999 50.070000 35559500 50.070000
2 2016-06-17 50.410000 50.430000 49.820000 50.130001 45670500 50.130001
3 2016-06-16 49.520000 50.470001 49.509998 50.389999 31101800 50.389999
4 2016-06-15 49.779999 50.119999 49.689999 49.689999 33559600 49.689999
In [2]:
data.plot(x='Date', y='Close', grid=True)
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x7fd3f1434630>
In [3]:
#################################################
#            2. Interactive Plots               #
#################################################
import bokeh.plotting as bp
import bokeh.io as bkio
bp.output_file("images/msft1.html", title="Bokeh Example (Static)")
In [4]:
# Bokeh has a matplotlib like gallery to showcase different plot styles
# http://bokeh.pydata.org/docs/gallery.html
p1 = bp.figure(title="Historical MSFT Quotes", 
              x_axis_type='datetime',
              plot_width=500, 
              plot_height=400)
p1.line(
    data['Date'], 
    data['Close'],
    color='#blue',
    legend='MSFT'
)

p2 = bp.figure(title="Historical MSFT Quotes", 
              x_axis_type='datetime',
              plot_width=500, 
              plot_height=400)
p2.line(
    data['Date'][1:200, ], 
    data['Close'],
    color='red',
    legend='MSFT'
)
Out[4]:
<bokeh.models.renderers.GlyphRenderer at 0x7fd3ed942390>
In [5]:
# Vertical laying out multiple plots 
p=bkio.vplot(p1, p2)
bp.show(p)


MSFT

Tagged on: ,

Leave a Reply