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_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_altair import show_test
import numpy as np
import pandas as pd

Autocorrelation

@viz_reg_test
def test_autocorrelation():
    np.random.seed(0)
    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_altair 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="site", color="year:O")


show_test(test_boxplot_color)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_altair 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:O", y="mean(yield)", hfacet="site")


show_test(test_barchart_color)
import altair_recipes as ar
from altair_recipes.common import viz_reg_test
from altair_recipes.display_altair 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)