In [ ]:
from __future__ import print_function
from bqplot import pyplot as plt
from bqplot import topo_load
from bqplot.interacts import panzoom
from numpy import *
import pandas as pd
import datetime as dt

In [ ]:
# initializing data to be plotted
random.seed(0)
size = 100
y_data = cumsum(random.randn(size) * 100.0)
y_data_2 = cumsum(random.randn(size))
y_data_3 = cumsum(random.randn(size) * 100.)

x = linspace(0.0, 10.0, size)

price_data = pd.DataFrame(cumsum(random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,
                          columns=['Security 1', 'Security 2'],
                          index=pd.date_range(start='01-01-2007', periods=150))

symbol = 'Security 1'
dates_all = price_data.index.values
final_prices = price_data[symbol].values.flatten()

Simple Plots

Line Chart


In [ ]:
plt.figure()
plt.plot(x, y_data)
plt.show()

Scatter Plot


In [ ]:
plt.figure(title='Scatter Plot with colors')
plt.scatter(y_data_2, y_data_3, color=y_data)
plt.show()

Histogram


In [ ]:
plt.figure()
plt.hist(y_data, colors=['OrangeRed'])
plt.show()

Bar Chart


In [ ]:
plt.figure()
bar_x=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U']
plt.bar(bar_x, y_data_3)
plt.show()

Pie Chart


In [ ]:
plt.figure()
d = abs(y_data_2[:5])
plt.pie(d)
plt.show()

OHLC


In [ ]:
dates = array(['2014-01-02T00:00:00.000000000+0000',
       '2014-01-03T00:00:00.000000000+0000',
       '2014-01-06T00:00:00.000000000+0000',
       '2014-01-07T00:00:00.000000000+0000',
       '2014-01-08T00:00:00.000000000+0000',
       '2014-01-09T00:00:00.000000000+0000',
       '2014-01-10T00:00:00.000000000+0000',
       '2014-01-13T00:00:00.000000000+0000',
       '2014-01-14T00:00:00.000000000+0000',
       '2014-01-15T00:00:00.000000000+0000',
       '2014-01-16T00:00:00.000000000+0000',
       '2014-01-17T00:00:00.000000000+0000',
       '2014-01-21T00:00:00.000000000+0000',
       '2014-01-22T00:00:00.000000000+0000',
       '2014-01-23T00:00:00.000000000+0000',
       '2014-01-24T00:00:00.000000000+0000',
       '2014-01-27T00:00:00.000000000+0000',
       '2014-01-28T00:00:00.000000000+0000',
       '2014-01-29T00:00:00.000000000+0000',
       '2014-01-30T00:00:00.000000000+0000'], dtype='datetime64[ns]')


prices = array([[ 187.21  ,  187.4   ,  185.2   ,  185.53  ],
       [ 185.83  ,  187.35  ,  185.3   ,  186.64  ],
       [ 187.15  ,  187.355 ,  185.3   ,  186.    ],
       [ 186.39  ,  190.35  ,  186.38  ,  189.71  ],
       [ 189.33  ,  189.4175,  187.26  ,  187.97  ],
       [ 189.02  ,  189.5   ,  186.55  ,  187.38  ],
       [ 188.31  ,  188.57  ,  186.28  ,  187.26  ],
       [ 186.26  ,  186.95  ,  183.86  ,  184.16  ],
       [ 185.06  ,  186.428 ,  183.8818,  185.92  ],
       [ 185.82  ,  188.65  ,  185.49  ,  187.74  ],
       [ 187.53  ,  188.99  ,  186.8   ,  188.76  ],
       [ 188.04  ,  190.81  ,  187.86  ,  190.09  ],
       [ 190.23  ,  190.39  ,  186.79  ,  188.43  ],
       [ 181.28  ,  183.5   ,  179.67  ,  182.25  ],
       [ 181.43  ,  183.72  ,  180.71  ,  182.73  ],
       [ 181.25  ,  182.8141,  179.64  ,  179.64  ],
       [ 179.605 ,  179.65  ,  177.66  ,  177.9   ],
       [ 178.05  ,  178.45  ,  176.16  ,  176.85  ],
       [ 175.98  ,  178.53  ,  175.89  ,  176.4   ],
       [ 177.17  ,  177.86  ,  176.36  ,  177.36  ]])

plt.figure()
plt.ohlc(dates, prices)
plt.show()

Map


In [ ]:
plt.figure()
plt.geo(map_data=topo_load('WorldMapData.json'))
plt.show()

Plotting Dates


In [ ]:
plt.figure()
plt.plot(dates_all, final_prices)
plt.show()

Editing existing axes properites


In [ ]:
## adding grid lines and changing the side of the axis in the figure above
plt.axes(options={'x': {'grid_lines': 'solid'}, 'y': {'side': 'right', 'grid_lines': 'dashed'}})

Advanced Usage

Multiple Marks on the same Figure


In [ ]:
plt.figure()
plt.plot(x, y_data_3, colors=['orange'])
plt.scatter(x, y_data, stroke='black')
plt.show()

Using marker strings in Line Chart


In [ ]:
mark_x = arange(10)
plt.figure(title='Using Marker Strings')
plt.plot(mark_x, 3 * mark_x + 5, 'y-.s') # color=yellow, line_style=dash_dotted, marker=square
plt.plot(mark_x ** 2, 'm:d') # color=magenta, line_style=None, marker=diamond
plt.show()

Partially changing the scales


In [ ]:
plt.figure()
plt.plot(x, y_data)

## preserving the x scale and changing the y scale
plt.scales(scales={'x': plt.Keep})
plt.plot(x, y_data_2, colors=['lightgreen'], axes_options={'y': {'side': 'right', 'color': 'lightgreen'}})
plt.show()

Adding a label to the chart


In [ ]:
plt.figure()
line = plt.plot(dates_all, final_prices)
plt.show()

In [ ]:
## adds the label to the figure created above
plt.label('Pie Day', x=dt.date(2007, 3, 14), y=final_prices.mean(), scales=line.scales,
          color='orange')

Changing context figure


In [ ]:
plt.figure(1)
plt.plot(x,y_data_3)
plt.show()

In [ ]:
plt.figure(2)
plt.plot(x[:20],y_data_3[:20])
plt.show()

Re-editing first figure


In [ ]:
## adds the new line to the first figure
plt.figure(1, title='New title')
plt.plot(x,y_data, colors=['orange'])

Viewing the properties of the figure


In [ ]:
marks = plt.current_figure().marks
marks[0].get_state()

Showing a second view of the first figure


In [ ]:
plt.show()

Clearing the figure


In [ ]:
### Clearing the figure above
plt.clear()

Deleting a figure and all its views.


In [ ]:
plt.show(2)

In [ ]:
plt.close(2)

Interactions in Pyplot


In [ ]:
def call_back(name, value):
    print(value)

Brush Selector


In [ ]:
plt.figure()
plt.scatter(y_data_2, y_data_3, default_colors=['orange'], stroke='black')

## click and drag on the figure to see the selector
plt.brush_selector(call_back)
plt.show(display_toolbar=False)

Fast Interval Selector


In [ ]:
plt.figure()
n= 100
plt.plot(arange(n), y_data_3)
## click on the figure to activate the selector
plt.int_selector(call_back)
plt.show(display_toolbar=False)

Brush Interval Selector with call back on brushing


In [ ]:
# click and drag on chart to make a selection
plt.brush_int_selector(call_back, 'brushing')