Examples
These examples are taken unedited from the test suite. Look at the body of each test to see how altair_recipes
can be used.
import altair as alt
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_pweave import show_test
from vega_datasets import data
Areaplot
@viz_reg_test
def test_areaplot():
return alt.vconcat(
*map(
lambda stack: ar.areaplot(
data.iowa_electricity(),
x="year",
y="net_generation",
color="source",
stack=stack,
),
ar.StackType,
)
)
show_test(test_areaplot)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_pweave import show_test
import numpy as np
import pandas as pd
Autocorrelation
@viz_reg_test
def test_autocorrelation():
data = pd.DataFrame(dict(x=np.random.uniform(size=100)))
return ar.autocorrelation(data, column="x", max_lag=15)
show_test(test_autocorrelation)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_pweave import show_test
from vega_datasets import data
Barchart
@viz_reg_test
def test_barchart_color():
source = data.barley()
return ar.barchart(source, x="year", y="mean(yield)", color=True)
show_test(test_barchart_color)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_pweave import show_test
from vega_datasets import data
Boxplot from melted data
@viz_reg_test
def test_boxplot_melted():
return ar.boxplot(data.iris(), columns=["petalLength"], group_by="species")
show_test(test_boxplot_melted)
Boxplot from cast data
@viz_reg_test
def test_boxplot_cast():
iris = data.iris()
return ar.boxplot(iris, columns=list(iris.columns[:-1]))
show_test(test_boxplot_cast)
Boxplot with color
@viz_reg_test
def test_boxplot_color():
source = data.barley()
return ar.boxplot(
source,
columns=["yield"],
group_by="year",
color=True,
width=800 // len(source["site"].unique()),
).facet(column="site")
show_test(test_boxplot_color)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_pweave import show_test
import numpy as np
import pandas as pd
from vega_datasets import data
Heatmap
@viz_reg_test
def test_heatmap():
# Compute x^2 + y^2 across a 2D grid
x, y = np.meshgrid(range(-5, 6), range(-5, 6))
z = x ** 2 + y ** 2
# Convert this grid to columnar data expected by Altair
data = pd.DataFrame({"x": x.ravel(), "y": y.ravel(), "z": z.ravel()})
return ar.heatmap(data, x="x", y="y", color="z")
show_test(test_heatmap)
Count Heatmap
@viz_reg_test
def test_count_heatmap():
source = data.movies.url
return ar.heatmap(
source, x="IMDB Rating", y="Rotten Tomatoes Rating", color="", aggregate="count"
)
show_test(test_count_heatmap)