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

In [ ]:
size = 100
np.random.seed(0)

x_data = range(size)
y_data = np.random.randn(size)
y_data_2 = np.random.randn(size)
y_data_3 = np.cumsum(np.random.randn(size) * 100.)

Basic Bar Chart


In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data[:20], y=y_data[:20], scales={'x': x_ord, 'y': y_sc})
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

Changing the reference value from which the Bars are drawn


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

bar = Bars(x=x_data[:20], y=np.abs(y_data_2[:20]), scales={'x': x_ord, 'y': y_sc}, base=1.0)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

In [ ]:
# changing the base
bar.base = 2.0

In [ ]:
bar.align = 'right'

Bar Chart Properties


In [ ]:
# Increasing the spacing between the bars
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data[:20], y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

In [ ]:
# changing basic properties like stroke and opacity
bar.stroke = 'orange'
bar.opacities = [0.5, 0.2]

Stacked Bar Chart for 2-d data


In [ ]:
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]], 
           scales={'x': x_ord, 'y': y_sc}, padding=0.2)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

Grouped Bar Chart


In [ ]:
bar.type = 'grouped' # equivalent to saying 
# bar = Bars(x=x_data, y=y_data, scales={'x': x_ord, 'y': y_sc}, padding=0.2, type='grouped')

Modifying color mode


In [ ]:
## Color mode has 2 values. 'group' and 'element'. 
## 'group' means for every x all bars have same color.
## 'element' means for every dimension of y, all bars have same color.
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=[y_data[:20], y_data_2[:20]],
           scales={'x': x_ord, 'y': y_sc}, padding=0.2, color_mode='group')
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

In [ ]:
## for 1-d array for Y.
x_ord = OrdinalScale()
y_sc = LinearScale()

bar = Bars(x=x_data, y=y_data[:20], scales={'x': x_ord, 'y': y_sc}, padding=0.2, 
           color_mode='element', labels=['Bar Chart'], 
           display_legend=True)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')

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

Representing additional dimension using Color


In [ ]:
# In this example, the color is just the magnitude of the y data
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(scheme='Reds')

bar = Bars(x=x_data[:20], y=y_data[:20], color=np.abs(y_data[:20]),
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')

margin = dict(top=50, bottom=80, left=50, right=50)
fig = Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)
display(fig)

Adding color for 2-d data


In [ ]:
# By default color is applied along the axis=1
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(scheme='Reds')

y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]
color_data = np.mean(y_vals, axis=1)

bar = Bars(x=x_data, y=y_vals, color=color_data, 
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc}, padding=0.2,
           labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')

margin = dict(top=50, bottom=80, left=50, right=50)
fig = Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)
display(fig)

In [ ]:
# Applying color along the axis=0
x_ord = OrdinalScale()
y_sc = LinearScale()
col_sc = ColorScale(mid=0.0)

y_vals = [y_data[:20], y_data_2[:20], y_data_3[:20] / 100.0]
color_data = np.mean(y_vals, axis=0)

bar = Bars(x=x_data, y=y_vals, color=color_data, 
           scales={'x': x_ord, 'y': y_sc, 'color': col_sc},
           padding=0.2, color_mode='group', stroke='orange')
ax_x = Axis(scale=x_ord)
ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')
ax_c = ColorAxis(scale=col_sc, tick_format='0.2f')

margin = dict(top=50, bottom=80, left=50, right=50)
fig = Figure(marks=[bar], axes=[ax_x, ax_y, ax_c], fig_margin=margin)
display(fig)

In [ ]: