pyplot
is a context based functional API offering meaningful defaults. It's a concise API and very similar to matplotlib's pyplot. Users new to bqplot
should use pyplot
as a starting point. Users create figure and mark objects using pyplot
functions.
Steps for building plots in pyplot
:
plt.scales
functionaxes_options
argument in the marks' functionspyplot
functions like plot
, bar
, scatter
etc. (All the marks created will be automatically added to the figure object created in step 1)plt.show
function which renders the figure in the current context along with toolbar for panzoom etc.pyplot
also offers many helper functions. A few are listed here:
Let's look at the same examples which were created in the Object Model Notebook
In [ ]:
import bqplot.pyplot as plt
In [ ]:
# first, let's create two vectors x and y to plot using a Lines mark
import numpy as np
x = np.linspace(-10, 10, 100)
y = np.sin(x)
# 1. Create the figure object
fig = plt.figure(title='Simple Line Chart')
# 2. By default axes are created with basic defaults. If you want to customize the axes create
# a dict and pass it to `axxes_options` argument in the marks
axes_opts = {'x': {'label': 'X'},
'y': {'label': 'Y'}}
# 3. Create a Lines mark by calling plt.plot function
line = plt.plot(x=x, y=y, axes_options=axes_opts) # note that custom axes options are passed here
# 4. Render the figure using plt.show()
plt.show()
For creating other marks (like scatter, pie, bars, etc.), only step 2 needs to be changed. Lets look a simple example to create a bar chart:
In [ ]:
# first, let's create two vectors x and y to plot a bar chart
x = list('ABCDE')
y = np.random.rand(5)
# 1. Create the figure object
fig = plt.figure(title='Simple Bar Chart')
# 2. Customize the axes options
axes_opts = {'x': {'label': 'X', 'grid_lines': 'none'},
'y': {'label': 'Y', 'tick_format': '.0%'}}
# 3. Create a Bars mark by calling plt.bar function
bar = plt.bar(x=x, y=y, padding=.2, axes_options=axes_opts)
# 4. directly display the figure object created in step 1 (note that the toolbar no longer shows up)
fig
Mutiple marks can be rendered in a figure. It's as easy as creating marks one after another. They'll all be added to the same figure!
In [ ]:
# first, let's create two vectors x and y
import numpy as np
x = np.linspace(-10, 10, 25)
y = 3 * x + 5
y_noise = y + 10 * np.random.randn(25) # add some random noise to y
# 1. Create the figure object
fig = plt.figure(title='Scatter and Line')
# 3. Create line and scatter marks
# additional attributes (stroke_width, colors etc.) can be passed as attributes to the mark objects as needed
line = plt.plot(x=x, y=y, colors=['green'], stroke_width=3)
scatter = plt.scatter(x=x, y=y_noise, colors=['red'], stroke='black')
# setting x and y axis labels using pyplot functions. Note that these functions
# should be called only after creating the marks
plt.xlabel('X')
plt.ylabel('Y')
# 4. render the figure
fig
pyplot
is a simpler and an intuitive API. It's available for all the marks except MarketMap. It should be used in almost all the cases by default since it offers a less verbose API compared to the Object Model. Please refer to the mark examples using pyplot
and also this pyplot example notebook