In [ ]:
from bqplot import *
from IPython.display import display
import numpy as np
import pandas as pd

In [ ]:
price_data = pd.DataFrame(np.cumsum(np.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))
y_data = np.cumsum(np.random.randn(100))

Label positioned in data co-ordinates


In [ ]:
x_sc = LinearScale()
y_sc = LinearScale()

test_line = Lines(x=np.arange(10), y=y_data[:10], scales={'x': x_sc, 'y': y_sc})
test_label = Label(x=5.0, y=np.mean(y_data[:10]), scales={'x': x_sc, 'y': y_sc},
                   text='Test Label', font_size='16px', font_weight='bolder', color='orange')

ax_x = Axis(scale=x_sc)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

fig = Figure(marks=[test_line, test_label], axes=[ax_x, ax_y])
display(fig)

Setting the label attribute enable_move to True makes the label draggable


In [ ]:
test_label.enable_move = True

Label positioned in terms of Figure co-ordinates


In [ ]:
x_sc = LinearScale()
y_sc = LinearScale()

test_line = Lines(x=np.arange(10), y=y_data,  scales={'x': x_sc, 'y': y_sc})
test_label = Label(x=0.5, y=0.2, text='Test Label', font_size='16px',
                  font_weight='bolder', color='orange')

ax_x = Axis(scale=x_sc)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

fig = Figure(marks=[test_line, test_label], axes=[ax_x, ax_y])
display(fig)

In [ ]:
# Rotating the label
test_label.rotate_angle = 30

Label positioned at a Date value


In [ ]:
import datetime as dt
dt_sc = DateScale()
y_sc = LinearScale()

lines = Lines(x=price_data.index.values, y=price_data['Security 1'].values, scales={'x': dt_sc, 'y': y_sc})
label = Label(x=dt.date(2007, 3, 14), y=0.5, scales={'x': dt_sc}, text='Pi Day', color='orange')

ax_x = Axis(scale=dt_sc)
ax_y = Axis(scale=y_sc, orientation='vertical')

fig = Figure(marks=[lines, label], axes=[ax_x, ax_y])
display(fig)

In [ ]:
# Setting an offset in pixel
label.x_offset = 100