Exemplo n.º 1
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'
def plot_rental_by_time(df, time_unit = "weekday"):
    field_dict= {
        "weekday": "weekday_num",
        "month" : 'mnth'
    }

    sort_field  = field_dict[time_unit]
    casual_plt = alt.Chart(df).mark_bar().encode(y=alt.Y(time_unit, sort=alt.EncodingSortField(
        field=sort_field, order="ascending", op='sum')), x=alt.X('casual', title="Rental Count")).properties(title="Casual")


    registered_plt = alt.Chart(df).mark_bar().encode(y=alt.Y(time_unit, sort=alt.EncodingSortField(
        field=sort_field, order="ascending", op='sum')), x=alt.X('registered', title="Rental Count")).properties(title='Registered')


    total_plt = alt.Chart(df).mark_bar().encode(y=alt.Y(time_unit, sort=alt.EncodingSortField(
        field=sort_field, order="ascending", op='sum')), x=alt.X('cnt', title='Rental Count')).properties(title="Total")


    combined_plt = alt.vconcat(
        casual_plt, registered_plt, total_plt)
    combined_plt = combined_plt.resolve_scale(
        x='shared').properties(title="Rental Number by Weekday")
    return combined_plt