Exemplo n.º 1
0
def test_repeat():
    # wrapped repeat
    chart1 = alt.Chart('data.csv').mark_point().encode(
        x=alt.X(alt.repeat(), type='quantitative'),
        y='y:Q',
    ).repeat(
        ['A', 'B', 'C', 'D'],
        columns=2
    )

    dct1 = chart1.to_dict()

    assert dct1['repeat'] == ['A', 'B', 'C', 'D']
    assert dct1['columns'] == 2
    assert dct1['spec']['encoding']['x']['field'] == {'repeat': 'repeat'}

    # explicit row/col repeat
    chart2 = alt.Chart('data.csv').mark_point().encode(
        x=alt.X(alt.repeat('row'), type='quantitative'),
        y=alt.Y(alt.repeat('column'), type='quantitative')
    ).repeat(
        row=['A', 'B', 'C'],
        column=['C', 'B', 'A']
    )

    dct2 = chart2.to_dict()

    assert dct2['repeat'] == {'row': ['A', 'B', 'C'], 'column': ['C', 'B', 'A']}
    assert 'columns' not in dct2
    assert dct2['spec']['encoding']['x']['field'] == {'repeat': 'row'}
    assert dct2['spec']['encoding']['y']['field'] == {'repeat': 'column'}
Exemplo n.º 2
0
def test_repeat():
    # wrapped repeat
    chart1 = (alt.Chart("data.csv").mark_point().encode(
        x=alt.X(alt.repeat(), type="quantitative"),
        y="y:Q",
    ).repeat(["A", "B", "C", "D"], columns=2))

    dct1 = chart1.to_dict()

    assert dct1["repeat"] == ["A", "B", "C", "D"]
    assert dct1["columns"] == 2
    assert dct1["spec"]["encoding"]["x"]["field"] == {"repeat": "repeat"}

    # explicit row/col repeat
    chart2 = (alt.Chart("data.csv").mark_point().encode(
        x=alt.X(alt.repeat("row"), type="quantitative"),
        y=alt.Y(alt.repeat("column"), type="quantitative"),
    ).repeat(row=["A", "B", "C"], column=["C", "B", "A"]))

    dct2 = chart2.to_dict()

    assert dct2["repeat"] == {
        "row": ["A", "B", "C"],
        "column": ["C", "B", "A"]
    }
    assert "columns" not in dct2
    assert dct2["spec"]["encoding"]["x"]["field"] == {"repeat": "row"}
    assert dct2["spec"]["encoding"]["y"]["field"] == {"repeat": "column"}
Exemplo n.º 3
0
def test_chart_infer_types():
    data = pd.DataFrame({'x': pd.date_range('2012', periods=10, freq='Y'),
                         'y': range(10),
                         'c': list('abcabcabca')})

    def _check_encodings(chart):
        dct = chart.to_dict()
        assert dct['encoding']['x']['type'] == 'temporal'
        assert dct['encoding']['x']['field'] == 'x'
        assert dct['encoding']['y']['type'] == 'quantitative'
        assert dct['encoding']['y']['field'] == 'y'
        assert dct['encoding']['color']['type'] == 'nominal'
        assert dct['encoding']['color']['field'] == 'c'

    # Pass field names by keyword
    chart = alt.Chart(data).mark_point().encode(x='x', y='y', color='c')
    _check_encodings(chart)

    # pass Channel objects by keyword
    chart = alt.Chart(data).mark_point().encode(x=alt.X('x'), y=alt.Y('y'),
                                                color=alt.Color('c'))
    _check_encodings(chart)

    # pass Channel objects by value
    chart = alt.Chart(data).mark_point().encode(alt.X('x'), alt.Y('y'),
                                                alt.Color('c'))
    _check_encodings(chart)

    # override default types
    chart = alt.Chart(data).mark_point().encode(alt.X('x', type='nominal'),
                                                alt.Y('y', type='ordinal'))
    dct = chart.to_dict()
    assert dct['encoding']['x']['type'] == 'nominal'
    assert dct['encoding']['y']['type'] == 'ordinal'
Exemplo n.º 4
0
def _make_chart_type(chart_type):
    data = pd.DataFrame({
        "x": [28, 55, 43, 91, 81, 53, 19, 87],
        "y": [43, 91, 81, 53, 19, 87, 52, 28],
        "color": list("AAAABBBB"),
    })
    base = (alt.Chart(data).mark_point().encode(
        x="x",
        y="y",
        color="color",
    ))

    if chart_type in ["layer", "hconcat", "vconcat", "concat"]:
        func = getattr(alt, chart_type)
        return func(base.mark_square(), base.mark_circle())
    elif chart_type == "facet":
        return base.facet("color")
    elif chart_type == "facet_encoding":
        return base.encode(facet="color")
    elif chart_type == "repeat":
        return base.encode(alt.X(alt.repeat(),
                                 type="quantitative")).repeat(["x", "y"])
    elif chart_type == "chart":
        return base
    else:
        raise ValueError(
            "chart_type='{}' is not recognized".format(chart_type))
Exemplo n.º 5
0
def _make_chart_type(chart_type):
    data = pd.DataFrame({
        'x': [28, 55, 43, 91, 81, 53, 19, 87],
        'y': [43, 91, 81, 53, 19, 87, 52, 28],
        'color': list('AAAABBBB'),
    })
    base = alt.Chart(data).mark_point().encode(
        x='x',
        y='y',
        color='color',
    )

    if chart_type in ['layer', 'hconcat', 'vconcat', 'concat']:
        func = getattr(alt, chart_type)
        return func(base.mark_square(), base.mark_circle())
    elif chart_type == 'facet':
        return base.facet('color')
    elif chart_type == 'facet_encoding':
        return base.encode(facet='color')
    elif chart_type == 'repeat':
        return base.encode(
            alt.X(alt.repeat(), type='quantitative')
        ).repeat(
            ['x', 'y']
        )
    elif chart_type == 'chart':
        return base
    else:
        raise ValueError("chart_type='{}' is not recognized".format(chart_type))
Exemplo n.º 6
0
def test_chart_infer_types():
    data = pd.DataFrame(
        {
            "x": pd.date_range("2012", periods=10, freq="Y"),
            "y": range(10),
            "c": list("abcabcabca"),
        }
    )

    def _check_encodings(chart):
        dct = chart.to_dict()
        assert dct["encoding"]["x"]["type"] == "temporal"
        assert dct["encoding"]["x"]["field"] == "x"
        assert dct["encoding"]["y"]["type"] == "quantitative"
        assert dct["encoding"]["y"]["field"] == "y"
        assert dct["encoding"]["color"]["type"] == "nominal"
        assert dct["encoding"]["color"]["field"] == "c"

    # Pass field names by keyword
    chart = alt.Chart(data).mark_point().encode(x="x", y="y", color="c")
    _check_encodings(chart)

    # pass Channel objects by keyword
    chart = (
        alt.Chart(data)
        .mark_point()
        .encode(x=alt.X("x"), y=alt.Y("y"), color=alt.Color("c"))
    )
    _check_encodings(chart)

    # pass Channel objects by value
    chart = alt.Chart(data).mark_point().encode(alt.X("x"), alt.Y("y"), alt.Color("c"))
    _check_encodings(chart)

    # override default types
    chart = (
        alt.Chart(data)
        .mark_point()
        .encode(alt.X("x", type="nominal"), alt.Y("y", type="ordinal"))
    )
    dct = chart.to_dict()
    assert dct["encoding"]["x"]["type"] == "nominal"
    assert dct["encoding"]["y"]["type"] == "ordinal"
Exemplo n.º 7
0
def test_layer_encodings():
    chart = alt.LayerChart().encode(x="column:Q")
    assert chart.encoding.x == alt.X(shorthand="column:Q")