In [1]:
import pandas as pd
import brunel
cars = pd.read_csv("data/cars.csv")
cars.head(6)
Out[1]:
We import the Brunel module and create a couple of simple scatterplots. We use the brunel magic to do so
The basic format of each call to Brunel is simple; whether it is a single line or a set of lines (a cell magic), they are concatenated together, and the result interprested as one command.
This command must start with an ACTION
, but may have a set of options at the end specified as ACTION :: OPTIONS
.
ACTION
is the Brunel action string; OPTIONS
are key=value
pairs:
data
defines the pandas dataframe to use. If not specified, the pandas data that best fits the action command will be usedwidth
and height
may be supplied to set the resulting sizeFor details on the Brunel Action languages, see the Online Docs on Bluemix
In [2]:
%brunel data('cars') x(mpg) y(horsepower) color(origin) filter(horsepower) :: width=800, height=300
Out[2]:
In [3]:
%brunel bar data('cars') x(origin) y(mpg) mean(mpg) animate(year:6) :: width=800, height=300
Out[3]:
In [4]:
%brunel data('cars') edge yrange(origin, year) chord size(#count) color(origin) :: width=500, height=400
Out[4]:
In [5]:
%brunel data('cars') treemap x(origin, year, cylinders) color(mpg) mean(mpg) size(#count) label(cylinders) tooltip(#all):: width=900, height=600
Out[5]:
Since Brunel uses the data frame, we can modify or add to that object to show data in different ways. In the following example we apply a function that takes a name and sees if it matches one of a set of sub-strings. We map this function to the car names to create a new column consisting of the names that match either "Ford" or "Buick", and use that in our Brunel action.
Because the Brunel action is long -- we are adding some CSS styling, we split it into two parts for convenience.
In [6]:
def identify(x, search):
for y in search:
if y.lower() in x.lower(): return y
return None
cars['Type'] = cars.name.map(lambda x: identify(x, ["Ford", "Buick"]))
In [7]:
%%brunel data('cars') x(engine) y(mpg) color(Type) style('size:50%; fill:#eee') +
x(engine) y(mpg) color(Type) text style('text {font-size:14; font-weight:bold; fill:darker}')
:: width=800, height=800
Out[7]:
In [ ]: