Example using bqplot

bqplot is an interactive framework for the Jupyter Notebook. Together with vaex it enables interactive plots, meaning you can zoom in and out, and do selections from the plot. The plot will also update for instance when you change the selection programmatically (Using Dataset.select ), or when done from the gui.

Installing bqplot

See bqplot installation instructions, or for pip:

$ pip install bqplot
$ jupyter nbextension enable --py bqplot

And anaconda/conda

$ conda install -c conda-forge bqplot

In [7]:
import bqplot.pyplot as plt

In [6]:
import bqplot
bqplot._version.version_info


Out[6]:
(0, 9, 0, 'b10')

In [3]:
bqplot.__file__


Out[3]:
'/Users/maartenbreddels/anaconda3/lib/python3.5/site-packages/bqplot/__init__.py'

In [9]:
x = np.linspace(0, 1, 10)
y = x**2
plt.scatter(x, y)
plt.show()



In [1]:
# only used in develop mode (remove this)
%load_ext autoreload
%autoreload 2


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

In [5]:
import vaex as vx
import ipywidgets as widgets
import numpy as np

In [4]:
# use the taxi dataset
ds = vx.datasets.nyctaxi_yellow_2015_jan.fetch()
# or do whole 2015
# ds = vx.datasets.nyctaxi_2015.get()
# we'll show pickup and dropoff locations
pickups = ds("pickup_longitude", "pickup_latitude")
dropoffs = ds("dropoff_longitude", "dropoff_latitude")
# default region we'll look at
geo_limits = lonrange, latrange = (-74.0655186833, -73.8048761181), (40.6444424556, 40.9050850209)
print("{:,} rows".format(len(ds)))


12,484,333 rows

In [5]:
(w1.__class__.__base__)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-b45fb8be76ba> in <module>()
----> 1 (w1.__class__.__base__)

NameError: name 'w1' is not defined

In [6]:
pickups.plot_bq(size=512, limits=geo_limits, f=np.log1p)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-e976c098b318> in <module>()
----> 1 pickups.plot_bq(size=512, limits=geo_limits, f=np.log1p)

/Users/maartenbreddels/vaex/src/vaex/vaex/legacy.py in plot_bq(self, grid, size, limits, square, center, weight, figsize, aspect, f, fig, axes, xlabel, ylabel, title, group_by, group_limits, group_colors, group_labels, group_count, cmap, scales, tool_select, bq_cleanup, **kwargs)
    792                 import ipywidgets as widgets
    793                 import bqplot as bq
--> 794                 f = _parse_f(f)
    795                 limits = self.limits(limits)
    796                 import vaex.ext.bqplot

NameError: name '_parse_f' is not defined

Example 2

We will now plot both the pickup and dropoff locations, link their scales (see bqplot documentation), and enable selections (using tool_select=True). When a selection is made, the plot will be updated.


In [7]:
size = 512
# plot bq returns a ipywigets VBox object, the first child object is the figure, and we use that to connect
# the scales of the two figures
w1 = pickups.plot_bq(size=size, limits=geo_limits, tool_select=True, title="Pickups", f=np.log1p)
fig = w1.children[0]
scales = fig.marks[-1].scales
w2 = dropoffs.plot_bq(size=size, limits=geo_limits, tool_select=True, scales=scales, title="Dropoffs", f=np.log1p)
widgets.HBox([w1, w2])


0.6.1
0.6.1

In [56]:
# selecting work dropoff hours
ds.select("(pickup_hour >= 7) & (pickup_hour <= 9)")

In [75]:
# here is where the nightlive is
ds.select("(pickup_hour >= 23) | (pickup_hour <= 4)")

In [58]:
b = widgets.Button(description="Blaat")

In [59]:
b


<ipywidgets.widgets.widget_button.Button object at 0x3c1edd850>
<ipywidgets.widgets.widget_button.Button object at 0x3c1edd850>

In [60]:
b


<ipywidgets.widgets.widget_button.Button object at 0x3c1edd850>

In [61]:
def f(arg):
    print arg
b.on_click(f)

In [62]:
b.description = "Hoeba"

In [65]:
@widgets.interact(bla=["aap", "noot"])
def f(i=1., j=1, bla="aap"):
    print i, j, bla


1.0 1 aap

In [66]:
t = widgets.Text()

In [67]:
t

In [68]:
t.value = "Hallo"

In [69]:
t2 = widgets.Text()

In [70]:
t2

In [73]:
widgets.ColorPicker()

In [ ]: