Python altair.condition() Examples

The following are 5 code examples of altair.condition(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module altair , or try the search function .
Example #1
Source File: plot.py    From retentioneering-tools with Mozilla Public License 2.0 6 votes vote down vote up
def altair_step_matrix(diff, plot_name=None, title='', vmin=None, vmax=None, font_size=12, **kwargs):
    heatmap_data = diff.reset_index().melt('index')
    heatmap_data.columns = ['y', 'x', 'z']
    table = alt.Chart(heatmap_data).encode(
        x=alt.X('x:O', sort=None),
        y=alt.Y('y:O', sort=None)
    )
    heatmap = table.mark_rect().encode(
        color=alt.Color(
            'z:Q',
            scale=alt.Scale(scheme='blues'),
        )
    )
    text = table.mark_text(
        align='center', fontSize=font_size
    ).encode(
        text='z',
        color=alt.condition(
            abs(alt.datum.z) < 0.8,
            alt.value('black'),
            alt.value('white'))
    )
    heatmap_object = (heatmap + text).properties(
        width=3 * font_size * len(diff.columns),
        height=2 * font_size * diff.shape[0]
    )
    return heatmap_object, plot_name, None, diff.retention.retention_config 
Example #2
Source File: test_core.py    From altair with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_infer_encoding_types_with_condition(channels):
    args, kwds = _getargs(
        x=alt.condition("pred1", alt.value(1), alt.value(2)),
        y=alt.condition("pred2", alt.value(1), "yval"),
        strokeWidth=alt.condition("pred3", "sval", alt.value(2)),
    )
    expected = dict(
        x=channels.XValue(2, condition=channels.XValue(1, test="pred1")),
        y=channels.Y("yval", condition=channels.YValue(1, test="pred2")),
        strokeWidth=channels.StrokeWidthValue(
            2, condition=channels.StrokeWidth("sval", test="pred3")
        ),
    )
    assert infer_encoding_types(args, kwds, channels) == expected 
Example #3
Source File: covid19_dataviz.py    From traffic with MIT License 5 votes vote down vote up
def airline_chart(
    source: alt.Chart, subset: List[str], name: str, loess=True
) -> alt.Chart:

    chart = source.transform_filter(
        alt.FieldOneOfPredicate(field="airline", oneOf=subset)
    )

    highlight = alt.selection(
        type="single", nearest=True, on="mouseover", fields=["airline"]
    )

    points = (
        chart.mark_point()
        .encode(
            x="day",
            y=alt.Y("rate", title="# of flights (normalized)"),
            color=alt.Color("airline", legend=alt.Legend(title=name)),
            tooltip=["day", "airline", "count"],
            opacity=alt.value(0.3),
        )
        .add_selection(highlight)
    )

    lines = chart.mark_line().encode(
        x="day",
        y="rate",
        color="airline",
        size=alt.condition(~highlight, alt.value(1), alt.value(3)),
    )
    if loess:
        lines = lines.transform_loess(
            "day", "rate", groupby=["airline"], bandwidth=0.2
        )

    return lines + points 
Example #4
Source File: covid19_dataviz.py    From traffic with MIT License 5 votes vote down vote up
def airport_chart(source: alt.Chart, subset: List[str], name: str) -> alt.Chart:

    chart = source.transform_filter(
        alt.FieldOneOfPredicate(field="airport", oneOf=subset)
    )

    highlight = alt.selection(
        type="single", nearest=True, on="mouseover", fields=["airport"]
    )

    points = (
        chart.mark_point()
        .encode(
            x="day",
            y=alt.Y("count", title="# of departing flights"),
            color=alt.Color("airport", legend=alt.Legend(title=name)),
            tooltip=["day", "airport", "city", "count"],
            opacity=alt.value(0.3),
        )
        .add_selection(highlight)
    )

    lines = (
        chart.mark_line()
        .encode(
            x="day",
            y="count",
            color="airport",
            size=alt.condition(~highlight, alt.value(1), alt.value(3)),
        )
        .transform_loess("day", "count", groupby=["airport"], bandwidth=0.2)
    )

    return lines + points 
Example #5
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def heatmap(data, vmin=None, vmax=None, annot=None, fmt='.2g'):

    # We always want to have a DataFrame with semantic information
    if not isinstance(data, pd.DataFrame):
        matrix = np.asarray(data)
        data = pd.DataFrame(matrix)

    melted = data.stack().reset_index(name='Value')

    x = data.columns.name
    y = data.index.name

    heatmap = alt.Chart(melted).mark_rect().encode(
        alt.X('{x}:O'.format(x=x), scale=alt.Scale(paddingInner=0)),
        alt.Y('{y}:O'.format(y=y), scale=alt.Scale(paddingInner=0)),
        color='Value:Q'
    )
    
    if not annot:
        return heatmap

    # Overlay text
    text = alt.Chart(melted).mark_text(baseline='middle').encode(
        x='{x}:O'.format(x=x),
        y='{y}:O'.format(y=y),
        text=alt.Text('Value', format=fmt),
        color=alt.condition(alt.expr.datum['Value'] > 70,
                            alt.value('black'),
                            alt.value('white'))
    )
    return heatmap + text