title | layout |
---|---|
Lecture 4 |
lecture |
- What is the visualization trying to show?
- What are its methods?
- What are the strengths / weaknesses?
- https://gizmodo.com/observatories-across-the-world-announce-groundbreaking-1819500578
- https://gizmodo.com/let-s-break-down-what-that-monumental-neutron-star-coll-1819613829
- Transformations
- Colors and color mapping
- HSV/RGB/etc
- Image visualization
- Choose an accessible, appropriate colormap
- "Am I showing different things?"
- "Can these things be compared directly?"
- "Do I want to demonstrate deviation or gradiation?"
- Mapping between "data space" and "color space" requires normalization and
color mapping
- Normalization:
$f(v) => v' \in [0, 1]$ - Color mapping:
$g(v) => RGBA$
- Normalization:
- Comparing Datasets
- Traitlets in IPython/Jupyter
- Visualization Engines: bqplot
- Among Items
- One Variable, Few Categories: Column, or collection of bars
- Two Variables: Variable Width Column Chart
- Many variables: Embedded table or charts
- Changing Over Time
- Many Periods, non-cyclical: Line chart
- Few Periods: Column or Line (depending on number of categories)
- pandas.pydata.org
- Support for indexing, multi-indexing
- Data structures
- Series
- DataFrame
- Panel
- Groupby, select, aggregation features
- IO features
- Reading/writing CSV, HDF5
- Loading data from remote sources
This week we'll be looking at two new visualization engines.
bqplot
vega-lite
Our first engine, bqplot
, is a Jupyter-based interactive plotting system.
It presents two principal interfaces:
pyplot
-like interface, for making the transition from matplotlib easier- An object-oriented API for constructing interactive visualizations
We will be using the latter far more frequently than the former.
Before we dig into bqplot
specifically, we will be examining a handful of
methods by which we can provide interaction as-is in Jupyter.
There are two underlying libraries we utilize for interactivity in Jupyter.
The first, traitlets
, provides methods for datatype-verification and
"watching" for changes.
import traitlets
class MyObject(traitlets.HasTraits):
name = traitlets.Unicode()
age = traitlets.Int()
my_obj = MyObject(name = "Weezer", age = 26)
Once we have an object that has traits, we can watch that object for changes.
def name_changed(change):
print(change['new'])
my_obj.observe(name_changed, ['name'])
In this case, we are watching the trait name
for changes. When a change
occurs, the function name_changed
is called. The argument is a dict with
these values:
new
: the new value the trait hasold
: the previous valuetype
: the type of changeowner
: the object that owns this traitname
: the name of the trait
We can use the ipywidgets
library to build out widgets in Jupyter notebooks.
These widgets can be quite extensive with many different operations;
additionally, they can have substantial CSS styling.
We've used simple examples before. For instance, we can create an interactive function very easily:
import ipywidgets
@ipywidgets.interact(name = ['Weezer', 'Nerf Herder', 'Mustard Plug'])
def print_bandname(name):
print(name)
This creates a dropdown that we can select an item from, which is supplied.
What this is doing implicitly is creating a widget with a value
attribute,
and whenever that value
is changed, the function is called again.
Automatically creating widgets using @ipywidgets.interact
is very handy and
useful for quick operations, but we can do this more deliberately as well.
There are a number of widgets available in ipywidgets
already:
IntSlider
,FloatSlider
,IntRangeSlider
,FloatRangeSlider
,IntProgress
andFloatProgress
all display or allow the user to choose values.IntText
,FloatText
,BoundedIntText
andBoundedFloatText
let the user input explicit values to a widget.
There are additional widget types that can provide indicators or restricted selections.
ToggleButton
,Checkbox
andValid
provide boolean indicators;Valid
is read-only.- For selection, there are
Dropdown
,RadioButtons
,Select
,SelectionSlider
and several others. - Strings can be provided using
Text
,TextArea
andHTML
. - Actions can be enabled through
Button
objects.
Widgets can also be laid out using HBox
, VBox
, Tab
, and Accordion
.
In addition to watching for changes, we can watch for events and we can link two (or more) values between different widgets.
The special method on_click
on a Button
allows for a function to be called
when something is clicked. We can also link using ipywidgets.link
and
supplying traits. For example:
m = MyObject(name = "Weezer", age=26)
l = ipywidgets.Label()
ipywidgets.link((m, 'name'), (l, 'value'))
display(l)
m.name = 'Nerf Herder'
Exercise: Add a button and make this change occur when clicked.
Now that we've learned a bit about widgets, we can start to dig into bqplot
.
bqplot
is based around traitlets and widget objects; every object you work
with will have traits and may be represented as a widget.
When we use bqplot
we will be constructing Figure
objects, which will
contain marks
and axes
. To use these, we will build mark objects (Bars
,
Lines
, Scatter
, Map
, etc) and describe the relationship between points
using Scale
objects.
We will be building out these objects and their relationships to develop interactivity.
- A mark is some mechanism for displaying data. For example, we might have
data that has a set of x and y values, which we can use
Lines
to represent. Scale
objects describe relationships between visual attributes (position) and data values.Axis
objects are where data are placed.Figure
objects contain marks and axes, as well as interaction.
Our first example will be a simple lineplot.
import bqplot
import numpy as np
x = np.arange(100)
y = np.random.random(100) + 5
x_sc = bqplot.LinearScale()
y_sc = bqplot.LinearScale()
lines = bqplot.Lines(x = x, y = y, scales = {'x': x_sc, 'y': y_sc})
ax_x = bqplot.Axis(scale = x_sc, label = 'X value')
ax_y = bqplot.Axis(scale = y_sc, label = 'Y value', orientation = 'vertical')
fig = bqplot.Figure(marks = [lines], axes = [ax_x, ax_y])
display(fig)
- Using traitlets, widgets and bqplot, build a notebook that:
- Uses the UFO datasets
- Allows changing the x and y fields on a scatter plot from the UFO dataset
- Displays tooltips when hovering over individual items
- Build a second widget that displays binned, aggregate values where you can change:
- The field to "bin"
- The method of aggregation (sum, mean, min, max, count)
- The number of bins
Today we are going to build comparisons with our (virtual) hands.
- A Bit More Matplotlib
- Patches and elements
- "Projections"
- Polar projections
- Traitlets