Repository: https://github.com/bloomberg/bqplot

Installation:

conda install -c conda-forge bqplot

Base plot


In [ ]:
import numpy as np
import bqplot.pyplot as plt

In [2]:
x = np.linspace(0, 10, 20)
y = x**3

fig = plt.figure(animation_duration=1000)
scatter = plt.scatter(x, y)
plt.show()


Animations


In [3]:
scatter.y = np.cos(x)

In [4]:
scatter.y = np.exp(x)

Selection


In [5]:
from bqplot.interacts import (
    FastIntervalSelector, IndexSelector, BrushIntervalSelector,
    BrushSelector, MultiSelector, LassoSelector,
)

In [6]:
scatter.selected_style = {'opacity': '1'}
scatter.unselected_style = {'opacity': '0.2'}
brushintsel = BrushIntervalSelector(scale=scatter.scales['x'], marks=[scatter])
fig.interaction = brushintsel

In [7]:
brushintsel


Out[7]:
BrushIntervalSelector(marks=[Scatter(colors=['steelblue'], interactions={'hover': 'tooltip'}, scales={'x': LinearScale(max=9.509803921568626, min=-0.49019607843137347), 'y': LinearScale(max=816.2650372608598, min=-183.7349627391402)}, scales_metadata={'x': {'orientation': 'horizontal', 'dimension': 'x'}, 'y': {'orientation': 'vertical', 'dimension': 'y'}, 'color': {'dimension': 'color'}, 'size': {'dimension': 'size'}, 'opacity': {'dimension': 'opacity'}, 'rotation': {'dimension': 'rotation'}, 'skew': {'dimension': 'skew'}}, selected_style={'opacity': '1'}, tooltip_style={'opacity': 0.9}, unselected_style={'opacity': '0.2'}, x=array([ 0.        ,  0.52631579,  1.05263158,  1.57894737,  2.10526316,
        2.63157895,  3.15789474,  3.68421053,  4.21052632,  4.73684211,
        5.26315789,  5.78947368,  6.31578947,  6.84210526,  7.36842105,
        7.89473684,  8.42105263,  8.94736842,  9.47368421, 10.        ]), y=array([1.00000000e+00, 1.69268460e+00, 2.86518116e+00, 4.84984802e+00,
       8.20926306e+00, 1.38956932e+01, 2.35210258e+01, 3.98136782e+01,
       6.73920000e+01, 1.14073401e+02, 1.93090288e+02, 3.26840958e+02,
       5.53238656e+02, 9.36458553e+02, 1.58512897e+03, 2.68312340e+03,
       4.54168166e+03, 7.68763460e+03, 1.30127407e+04, 2.20264658e+04]))], scale=LinearScale(max=9.509803921568626, min=-0.49019607843137347), selected=array([], dtype=object))

In [8]:
scatter.selected


Out[8]:
[9, 10, 11, 12, 13, 14, 15, 16]

Pie


In [9]:
import numpy as np
from bqplot import Pie, Figure

In [10]:
data = np.random.rand(3)
pie = Pie(sizes=data, display_labels='outside')
fig = Figure(marks=[pie], animation_duration=1000)
fig



In [13]:
n = np.random.randint(1, 10)
pie.sizes = np.random.rand(n)

Scatter plot with a color scale


In [14]:
import pandas as pd
from bqplot import *

In [15]:
price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[1.0, -0.8], [-0.8, 1.0]]), axis=0) + 100,
                          columns=['Security 1', 'Security 2'], index=pd.date_range(start='01-01-2007', periods=150))
dates_all = price_data.index.values
symbols = ['Security 1', 'Security 2']
sec1_levels = np.array(price_data[symbols[0]].values.flatten())
log_sec1 = np.log(sec1_levels)
sec1_returns = log_sec1[1:] - log_sec1[:-1]
sec2_levels = np.array(price_data[symbols[1]].values.flatten())

sc_x = DateScale()
sc_y = LinearScale()

sc_c1 = ColorScale()
scatter = Scatter(x=dates_all, y=sec2_levels, color=sec1_returns,
                  scales={'x': sc_x, 'y': sc_y, 'color': sc_c1}, 
                  stroke='black')

ax_y = Axis(label='Security 2', scale=sc_y, 
            orientation='vertical', side='left')

ax_x = Axis(label='Date', scale=sc_x, num_ticks=10, label_location='end')
ax_c = ColorAxis(scale=sc_c1, tick_format='0.2%', label='Returns', orientation='vertical', side='right')

m_chart = dict(top=50, bottom=70, left=50, right=100)

Figure(axes=[ax_x, ax_c, ax_y], marks=[scatter], fig_margin=m_chart,
       title='Scatter of Security 2 vs Dates')


Interactions with other widgets


In [16]:
import numpy as np
import bqplot.pyplot as plt
from bqplot import *
from traitlets import observe
from ipywidgets import VBox, Button

In [17]:
axes_options = {'x': {'label': 'x'}, 'y': {'label': 'y'}}

x = np.arange(100)
y = np.cumsum(np.random.randn(2, 100), axis=1)

fig = plt.figure(animation_duration=1000)
lines = plt.plot(x=x, y=y, colors=['red', 'green'], axes_options=axes_options)

def generate(_):
    lines.y = np.cumsum(np.random.randn(2, 100), axis=1)

button = Button(description='Generate data', button_style='success')
button.on_click(generate)

VBox([fig, button])


Clean


In [18]:
from ipywidgets import Widget
Widget.close_all()

In [ ]: