import altair.vegalite.v2 as vl
from altair.datasets import load_dataset
cars = load_dataset('cars')
cars.head()
Acceleration | Cylinders | Displacement | Horsepower | Miles_per_Gallon | Name | Origin | Weight_in_lbs | Year | |
---|---|---|---|---|---|---|---|---|---|
0 | 12.0 | 8 | 307.0 | 130.0 | 18.0 | chevrolet chevelle malibu | USA | 3504 | 1970-01-01 |
1 | 11.5 | 8 | 350.0 | 165.0 | 15.0 | buick skylark 320 | USA | 3693 | 1970-01-01 |
2 | 11.0 | 8 | 318.0 | 150.0 | 18.0 | plymouth satellite | USA | 3436 | 1970-01-01 |
3 | 12.0 | 8 | 304.0 | 150.0 | 16.0 | amc rebel sst | USA | 3433 | 1970-01-01 |
4 | 10.5 | 8 | 302.0 | 140.0 | 17.0 | ford torino | USA | 3449 | 1970-01-01 |
vl.data_transformers.enable('json')
spec = {
"data": vl.pipe(cars, vl.data_transformers.get()),
"vconcat": [{
"selection": {
"brush": {"type": "interval"}
},
"mark": "point",
"encoding": {
"x": {"field": "Horsepower","type": "quantitative"},
"y": {"field": "Miles_per_Gallon","type": "quantitative"}
}
}, {
"transform": [
{"filter": {"selection": "brush"}}
],
"mark": "point",
"encoding": {
"x": {
"field": "Acceleration", "type": "quantitative",
"scale": {"domain": [0,25]}
},
"y": {
"field": "Displacement","type": "quantitative",
"scale": {"domain": [0, 500]}
}
}
}]
}
To render in the classic notebook run this line:
vg.renderers.enable('notebook')
To render in JupyterLab and nteract, run this
vg.renderers.enable('default')
vl.renderers.get()(spec)
({'application/vnd.vegalite.v2+json': {'data': {'format': {'type': 'json'}, 'url': '/files/github/altair-viz/altair/notebooks/altair-data-0004ed27-fa48-4358-8bac-bea454ea1c74.json'}, 'vconcat': [{'encoding': {'x': {'field': 'Horsepower', 'type': 'quantitative'}, 'y': {'field': 'Miles_per_Gallon', 'type': 'quantitative'}}, 'mark': 'point', 'selection': {'brush': {'type': 'interval'}}}, {'encoding': {'x': {'field': 'Acceleration', 'scale': {'domain': [0, 25]}, 'type': 'quantitative'}, 'y': {'field': 'Displacement', 'scale': {'domain': [0, 500]}, 'type': 'quantitative'}}, 'mark': 'point', 'transform': [{'filter': {'selection': 'brush'}}]}]}, 'text/plain': '<VegaLite 2 object>'}, {})
vl.vegalite(spec, validate=True)
<VegaLite 2 object>
vl.renderers.enable('json')
vl.vegalite(spec, validate=True)
<VegaLite 2 object>