In [ ]:
import numpy as np
import bqplot.pyplot as plt
from bqplot import CATEGORY10, ColorScale

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 [ ]:
plt.figure(title='Bar Chart')
plt.bar(np.arange(10), np.random.rand(10))
plt.show()

Horizontal Bar Chart

To generate a horizontal bar chart, pass orientation='horizontal' to the bar.


In [ ]:
plt.figure(title='Horizontal Bar Chart')
plt.bar(np.arange(10), np.random.uniform(-1, 1, 10), orientation='horizontal')
plt.show()

Changing the reference value from which the Bars are drawn


In [ ]:
fig = plt.figure()
# assign the output of the plt.bar to a mark object
bar = plt.bar(x_data[:20], np.abs(y_data_2[:20]), base=1.0)
# render the figure
fig

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

In [ ]:
bar.align = 'right'

Bar Chart Properties


In [ ]:
# Increasing the spacing between the bars using padding property
fig = plt.figure()
bar = plt.bar(x_data[:20], y_data[:20], padding=0.3)
fig

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

In [ ]:
bar.orientation = 'horizontal'
fig.axes[0].orientation = 'vertical'
fig.axes[1].orientation = 'horizontal'

Stacked Bar Chart for 2-d data


In [ ]:
fig = plt.figure()
bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], 
              padding=0.2,
              colors=CATEGORY10)
fig

Grouped Bar Chart


In [ ]:
bar.type = 'grouped' # equivalent to saying
# plt.bar(x_data, y_data, padding=0.2, type='grouped')

In [ ]:
fig = plt.figure()
bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], 
              padding=0.2, 
              colors=CATEGORY10, 
              orientation='horizontal')
fig

In [ ]:
bar.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.
fig = plt.figure()
bar = plt.bar(x_data, [y_data[:20], y_data_2[:20]], padding=0.2, colors=CATEGORY10, color_mode='group')
fig

In [ ]:
## for 1-d array for Y.
fig = plt.figure()
bar = plt.bar(x_data, y_data[:20], padding=0.2, color_mode='element', 
              labels=['Values'], display_legend=True)
fig

Representing additional dimension using Color


In [ ]:
# In this example, the color is just the magnitude of the y data
fig = plt.figure()
# add a 'reds' color scale 
plt.scales(scales={'color': ColorScale(scheme='Reds')})

bar = plt.bar(x_data[:20], y_data[:20], color=np.abs(y_data[:20]), padding=0.2)

# give enough bottom margin to accommodate the color axis
fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)

fig

Adding color for 2-d data


In [ ]:
# By default color is applied along the axis=1
fig = plt.figure()
plt.scales(scales={'color': 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 = plt.bar(x_data, y_vals, color=color_data, padding=0.2,
           labels=['Dim 1', 'Dim 2', 'Dim 3'], display_legend=True)

fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)
fig

In [ ]:
# Applying color along the axis=0
fig = plt.figure()
plt.scales(scales={'color': ColorScale(mid=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 = plt.bar(x_data, y_vals, color=color_data, padding=0.2, 
              color_mode='group', stroke='orange')
fig.fig_margin = dict(top=50, bottom=80, left=50, right=50)
fig