How to use the altair.condition function in altair

To help you get started, we’ve selected a few altair examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xoolive / traffic / scripts / covid19_dataviz.py View on Github external
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
github anitagraser / movingpandas / sandbox / summarize_prediction_results.py View on Github external
height=170
        )


    
    legend = alt.Chart(df).mark_rect().encode(
            #x=alt.X('past:N'),
            y=alt.Y('future:N', axis=alt.Axis(orient='right')),
            color=future_color
        ).add_selection(
            future_selection
        )
        
    context_selection = alt.selection_multi(fields=['context'])
    context_color = alt.condition(context_selection, alt.Color('future:N', legend=None), alt.value('lightgray'))
    map_color = alt.condition(context_selection, alt.value('#4c78a8'), alt.value('#f5f5f5'))
        
    scatter = alt.Chart(df, title='Along-track & Cross-track Error').mark_point(
            clip=True
        ).encode(
            alt.X('along_track_error', scale=alt.Scale(domain=(0, 5000))),
            alt.Y('cross_track_error', scale=alt.Scale(domain=(0, 5000))),
            color=context_color,
            tooltip=['context','past','future','along_track_error','cross_track_error']
        ).add_selection(
            context_selection
        ).transform_filter(
            future_selection
        ).properties(
            width=400,
            height=400
        )
github altair-viz / altair / altair / examples / selection_layer_bar_month.py View on Github external
===================
The plot below uses an interval selection, which causes the chart to include an interactive brush
(shown in grey). The brush selection parameterizes the red guideline, which visualizes the average
value within the selected interval.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.seattle_weather()
brush = alt.selection(type='interval', encodings=['x'])

bars = alt.Chart().mark_bar().encode(
    x='month(date):O',
    y='mean(precipitation):Q',
    opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7))
).add_selection(
    brush
)

line = alt.Chart().mark_rule(color='firebrick').encode(
    y='mean(precipitation):Q',
    size=alt.SizeValue(3)
).transform_filter(
    brush
)

alt.layer(bars, line, data=source)
github PythonCharmers / starborn / starborn / core.py View on Github external
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
github altair-viz / altair / altair / examples / multiple_interactions.py View on Github external
rating_select = alt.selection_single(fields=['MPAA_Rating'], bind=rating_radio, name="Rating")
rating_color_condition = alt.condition(rating_select,
                      alt.Color('MPAA_Rating:N', legend=None),
                      alt.value('lightgray'))

highlight_ratings = base.add_selection(
    rating_select
).encode(
    color=rating_color_condition
).properties(title="Radio Button Highlighting")

# Boolean selection for format changes
input_checkbox = alt.binding_checkbox()
checkbox_selection = alt.selection_single(bind=input_checkbox, name="Big Budget Films")

size_checkbox_condition = alt.condition(checkbox_selection,
                                        alt.SizeValue(25),
                                        alt.Size('Hundred_Million_Production:Q')
                                       )

budget_sizing = base.add_selection(
    checkbox_selection
).encode(
    size=size_checkbox_condition
).properties(title="Checkbox Formatting")

( filter_year | filter_genres) &  (highlight_ratings | budget_sizing  )
github inferne / notes / tensorflow / RecommendationSystems.py View on Github external
def visualize_movie_embeddings(data, x, y):
  nearest = alt.selection(
      type='single', encodings=['x', 'y'], on='mouseover', nearest=True,
      empty='none')
  base = alt.Chart().mark_circle().encode(
      x=x,
      y=y,
      color=alt.condition(genre_filter, "genre", alt.value("whitesmoke")),
  ).properties(
      width=600,
      height=600,
      selection=nearest)
  text = alt.Chart().mark_text(align='left', dx=5, dy=-5).encode(
      x=x,
      y=y,
      text=alt.condition(nearest, 'title', alt.value('')))
  return alt.hconcat(alt.layer(base, text), genre_chart, data=data)
github neptune-ml / neptune-lib / neptunelib / contrib / visualizations.py View on Github external
)

    line = alt.Chart().mark_line().encode(
        x=alt.X('x:Q',title='iteration'),
        y=alt.Y('y:Q', scale=alt.Scale(zero=False), title=channel_name),
        color=alt.Color('id:N', legend=None),
        opacity=alt.condition(legend_selection, alt.OpacityValue(1), alt.OpacityValue(0.0))
    )

    points = line.mark_point().encode(
        color=alt.condition(legend_selection, alt.Color('id:N', legend=None), alt.value('white')),
        opacity=alt.condition(nearest, alt.value(1), alt.value(0))
    )

    text = line.mark_text(align='left', dx=5, dy=-5).encode(
        text=alt.condition(nearest, 'y:Q', alt.value(' ')),
        opacity=alt.condition(legend_selection, alt.OpacityValue(1), alt.OpacityValue(0.0))
    )

    rules = alt.Chart().mark_rule(color='gray').encode(
        x='x:Q',
    ).transform_filter(
        nearest
    )

    bottom_view = alt.layer(line, selectors, points, rules, text,
                            width=800, height=400
    ).transform_filter(
        interval
    )

    combined = alt.hconcat(alt.vconcat(top_view, bottom_view),
github neptune-ml / neptune-lib / neptunelib / contrib / visualizations.py View on Github external
width=800,
    )


    exp_line = alt.Chart().mark_area(interpolate='step-after', size=5).encode(
        x=alt.X('finished:T', scale=alt.Scale()),
        y=alt.Y('daily_experiment_counts:Q',scale=alt.Scale(zero=False)),
        color=alt.ColorValue('pink'),
        opacity=alt.OpacityValue(0.5)
    ).transform_filter(
        brush
    )

    exp_points = exp_line.mark_point(filled=True).encode(
        color=alt.ColorValue('black'),
        opacity=alt.condition(nearest, alt.value(1), alt.value(0))
    )

    exp_text = exp_line.mark_text(align='left', dx=5, dy=-5, fontWeight='bold', size=15).encode(
        text=alt.condition(nearest, 'daily_experiment_counts:Q'.format(metric_name), alt.value(' ')),
        color=alt.ColorValue('black')
    )

    exp_rules = alt.Chart().mark_rule(color='gray').encode(
        x=alt.X('finished:T', scale=alt.Scale()),
    ).transform_filter(
        nearest
    )

    exps = alt.layer(exp_line, exp_points, exp_rules, exp_text).properties(
        height=400,
        width=800,
github altair-viz / altair / altair / examples / selection_histogram.py View on Github external
This chart shows an example of using an interval selection to filter the
contents of an attached histogram, allowing the user to see the proportion
of items in each category within the selection.
"""
# category: interactive charts
import altair as alt
from vega_datasets import data

source = data.cars()

brush = alt.selection(type='interval')

points = alt.Chart(source).mark_point().encode(
    x='Horsepower:Q',
    y='Miles_per_Gallon:Q',
    color=alt.condition(brush, 'Origin:N', alt.value('lightgray'))
).add_selection(
    brush
)

bars = alt.Chart(source).mark_bar().encode(
    y='Origin:N',
    color='Origin:N',
    x='count(Origin):Q'
).transform_filter(
    brush
)

points & bars
github altair-viz / altair / altair / examples / candlestick_chart.py View on Github external
"""
Candlestick Chart
=================
A candlestick chart inspired from `Protovis `_. 
This example shows the performance of the Chicago Board Options Exchange `Volatility Index `_ (VIX) 
in the summer of 2009. The thick bar represents the opening and closing prices, 
while the thin bar shows intraday high and low prices; if the index closed higher on a given day, the bars are colored green rather than red.
"""
# category: other charts
import altair as alt
from vega_datasets import data

source = data.ohlc()

open_close_color = alt.condition("datum.open <= datum.close",
                                 alt.value("#06982d"),
                                 alt.value("#ae1325"))

base = alt.Chart(source).encode(
    alt.X('date:T',
          axis=alt.Axis(
              format='%m/%d', 
              labelAngle=-45, 
              title='Date in 2009'
          )
    ),
    color=open_close_color
)

rule = base.mark_rule().encode(
    alt.Y(