In [ ]:
import bqplot
import numpy as np
from ipywidgets import link, FloatSlider
In [ ]:
N = 3 * int(1e5)
scale_x = bqplot.LinearScale(min=-5, max=5)
scale_y = bqplot.LinearScale(min=-5, max=5)
scale_size = bqplot.LinearScale(min=0, max=1)
x = np.random.normal(5, 4, N)
y = np.random.normal(0, 3, N)
size = np.random.rand(N) * 30 + 1
scatter = bqplot.ScatterGL(x=x, y=y,
size=size,
colors=['red', 'green', 'yellow'],
scales={
'x': scale_x, 'y': scale_y,
'size': scale_size
},
default_size=20,
marker='circle'
)
ax_x = bqplot.Axis(scale=scale_x, grid_lines='solid', label='X')
ax_y = bqplot.Axis(scale=scale_y, orientation='vertical', tick_format='0.2f', grid_lines='solid', label='Y')
fig = bqplot.Figure(marks=[scatter], axes=[ax_x, ax_y], title='Scatter GL', legend_location='bottom-right', animation_duration=2000)
scatter.stroke = 'black'
fig
In [ ]:
N = int(1e4)
with scatter.hold_sync():
scatter.x = np.random.normal(5, 4, N)
scatter.y = np.random.normal(0, 3, N)
In [ ]:
scatter.colors = ['blue']
In [ ]:
scatter.colors = ['blue', 'orange']
In [ ]:
scatter.marker = 'cross'
In [ ]:
scatter.marker = 'triangle-up'
In [ ]:
scatter.marker = 'triangle-down'
In [ ]:
scatter.marker = 'square'
In [ ]:
N = 3 * int(1e4)
scale_x = bqplot.LinearScale(min=-5, max=5)
scale_y = bqplot.LinearScale(min=-5, max=5)
scale_size = bqplot.LinearScale(min=0, max=1)
scale_color = bqplot.ColorScale(min=0, max=1, scheme='RdYlGn')
x = np.random.normal(5, 4, N)
y = np.random.normal(0, 3, N)
size = np.random.rand(N) * 30 + 1
color = np.random.rand(N)
scatter = bqplot.ScatterGL(x=x, y=y,
size=size,
color=color,
scales={
'x': scale_x, 'y': scale_y,
'size': scale_size,
'color': scale_color
},
default_size=20,
marker='circle'
)
scatter.selected_style = {'fill': 'blue'}
ax_x = bqplot.Axis(scale=scale_x, grid_lines='solid', label='X')
ax_y = bqplot.Axis(scale=scale_y, orientation='vertical', tick_format='0.2f', grid_lines='solid', label='Y')
fig = bqplot.Figure(marks=[scatter], axes=[ax_x, ax_y], title='Scatter GL', legend_location='bottom-right', animation_duration=2000)
panzoom = bqplot.interacts.PanZoom(scales={'x': [scale_x], 'y': [scale_y]})
fig.interaction = panzoom
scatter.stroke = 'black'
fig
In [ ]:
with scatter.hold_sync():
scatter.x = np.random.normal(5, 4, N)
scatter.y = np.random.normal(0, 3, N)
scatter.size = np.random.normal(20, 20, N)
In [ ]:
scatter.y = np.random.normal(0, 3, N)
In [ ]:
scatter.size = np.random.rand(N) * 50 + 1
In [ ]:
from ipywidgets import FloatSlider, link
slider = FloatSlider(min=0.1, max=10., value=scale_color.max)
link((slider, 'value'), (scale_color, 'max'))
slider
In [ ]:
scatter.stroke = None
In [ ]:
scatter.stroke_width = 5
In [ ]:
scatter.x = np.random.normal(5, 4, N)
In [ ]:
import numpy as np
import pandas as pd
from bqplot import (
Axis, ColorAxis, LinearScale, DateScale, DateColorScale, OrdinalScale,
OrdinalColorScale, ColorScale, ScatterGL, Lines, Figure, Tooltip, PanZoom
)
from ipywidgets import Label
In [ ]:
size = 100
price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[1.0, -0.8], [-0.8, 1.0]]), axis=0) + size,
columns=['Security 1', 'Security 2'], index=pd.date_range(start='01-01-2007', periods=150))
np.random.seed(0)
x_data = range(size)
y_data = np.cumsum(np.random.randn(size) * 100.0)
ord_keys = np.array(['A', 'B', 'C', 'D', 'E', 'F'])
ordinal_data = np.random.randint(5, size=size)
In [ ]:
symbols = ['Security 1', 'Security 2']
dates_all = price_data.index.values
dates_all_t = dates_all[1:]
sec1_levels = np.array(price_data[symbols[0]].values.flatten())
log_sec1 = np.log(sec1_levels)
sec1_returns = log_sec1
sec2_levels = np.array(price_data[symbols[1]].values.flatten())
In [ ]:
import bqplot
sc_x = DateScale()
sc_y = LinearScale()
scatt = ScatterGL(x=dates_all, y=sec2_levels, marker='square', stroke='black', scales={'x': sc_x, 'y': sc_y})
ax_x = Axis(scale=sc_x, label='Date')
ax_y = Axis(scale=sc_y, orientation='vertical', tick_format='0.0f', label='Security 2')
fig = Figure(marks=[scatt], axes=[ax_x, ax_y], animation_duration=5000)
panzoom = bqplot.interacts.PanZoom(scales={'x': [sc_x], 'y': [sc_y]})
fig.interaction = panzoom
fig
In [ ]:
# Changing the marker as
sc_x = LinearScale()
sc_y = LinearScale()
scatt = ScatterGL(x=x_data, y=y_data, names=np.arange(10),
scales={'x': sc_x, 'y': sc_y}, colors=['red'], marker='cross')
ax_x = Axis(scale=sc_x)
ax_y = Axis(scale=sc_y, orientation='vertical', tick_format='0.2f')
Figure(marks=[scatt], axes=[ax_x, ax_y], padding_x=0.025)
In [ ]:
scatt.marker = 'square'
In [ ]:
scatt.default_opacities = [0.3, 0.5, 1.]
In [ ]:
sc_x = DateScale()
sc_y = LinearScale()
sc_c1 = ColorScale()
scatter = ScatterGL(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')
In [ ]:
## setting the fill to be empty
scatter.stroke = None
scatter.fill = False
In [ ]:
## Setting the fill back
scatter.stroke = 'black'
scatter.fill = True
In [ ]:
## Changing the color to a different variable
scatter.color = sec2_levels
ax_c.tick_format = '0.0f'
ax_c.label = 'Security 2'
In [ ]:
## Changing the range of the color scale
sc_c1.colors = ['blue', 'green', 'orange']
In [ ]:
sc_x = LinearScale()
sc_y = LinearScale()
sc_c1 = DateColorScale(scheme='Reds')
scatter = ScatterGL(x=sec2_levels, y=sec1_levels, color=dates_all,
scales={'x': sc_x, 'y': sc_y, 'color': sc_c1}, default_size=128,
stroke='black')
ax_y = Axis(label='Security 1 Level', scale=sc_y, orientation='vertical', side='left')
ax_x = Axis(label='Security 2', scale=sc_x)
ax_c = ColorAxis(scale=sc_c1, label='Date', num_ticks=5)
m_chart = dict(top=50, bottom=80, left=50, right=50)
Figure(axes=[ax_x, ax_c, ax_y], marks=[scatter], fig_margin=m_chart)
In [ ]:
sc_x = LinearScale()
sc_y = LinearScale()
sc_y2 = LinearScale()
sc_size = LinearScale()
sc_opacity = LinearScale()
scatter2 = ScatterGL(x=sec2_levels, y=sec1_levels, size=sec1_returns,
scales={'x': sc_x, 'y': sc_y, 'size': sc_size},
default_size=128, colors=['orangered'], stroke='black')
ax_y = Axis(label='Security 1', scale=sc_y, orientation='vertical', side='left')
ax_x = Axis(label='Security 2', scale=sc_x)
Figure(axes=[ax_x, ax_y], marks=[scatter2])
In [ ]:
## Changing the opacity of the scatter
scatter2.default_opacities = [0.5, 0.3, 0.1]
In [ ]:
## Resetting the opacity and setting the opacity according to the date
scatter2.default_opacities = [1.0]
In [ ]:
sc_x = LinearScale()
sc_y = LinearScale()
sc_e = LinearScale()
sc_c = ColorScale(scheme='Reds')
x1 = np.linspace(-1, 1, 250)
y1 = np.linspace(-1, 1, 250)
x, y = np.meshgrid(x1,y1)
x, y = x.flatten(), y.flatten()
rot = x**2 + y**2
color=x-y
scatter = ScatterGL(scales={'x': sc_x, 'y': sc_y, 'color': sc_c, 'rotation': sc_e},
x=x, y=y, rotation=rot, color=color,
stroke="black", stroke_width=20, default_size=200,
marker='arrow', default_skew=0.5)
fig = Figure(marks=[scatter], animation_duration=1000)
panzoom = PanZoom(scales={'x': [sc_x], 'y': [sc_y]})
fig.interaction = panzoom
fig
In [ ]:
scatter.rotation = 1.0 / (x ** 2 + y ** 2 + 1)
In [ ]:
scatter.rotation = x**2 + y**2