How to use altair - 10 common examples

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 piccolbo / altair_recipes / altair_recipes / scatterplot.py View on Github external
height=600,
    width=800,
):
    """Generate many scatterplots.

    Based on several columns, pairwise.
    """
    opt_args = choose_kwargs(locals(), ["color", "tooltip"])

    assert group_by is None, "Long format not supported yet"
    return (
        alt.Chart(data, height=height // len(columns), width=width // len(columns))
        .mark_point(size=1 / len(columns), opacity=opacity)
        .encode(
            alt.X(alt.repeat("column"), type="quantitative"),
            alt.Y(alt.repeat("row"), type="quantitative"),
            **opt_args
        )
        .repeat(row=columns, column=columns)
    )
github JasonKessler / scattertext / scattertext / diachronic / BubbleDiachronicVisualization.py View on Github external
def visualize(display_df):
        viridis = ['#440154', '#472c7a', '#3b518b', '#2c718e', '#21908d', '#27ad81', '#5cc863', '#aadc32', '#fde725']
        import altair as alt
        color_scale = alt.Scale(
            domain=(display_df.dropna().trending.min(),
                    0,
                    display_df.dropna().trending.max()),
            range=[viridis[0], viridis[len(viridis) // 2], viridis[-1]]
        )

        return alt.Chart(display_df).mark_circle().encode(
            alt.X('variable'),
            alt.Y('term'),
            size='frequency',
            color=alt.Color('trending:Q', scale=color_scale),
        )
github altair-viz / altair / altair / examples / poly_fit.py View on Github external
rng = np.random.RandomState(1)
x = rng.rand(40) ** 2
y = 10 - 1. / (x + 0.1) + rng.randn(40)
df = pd.DataFrame({'x': x, 'y': y})

# Define the degree of the polynomial fit
degree_list = [1, 3, 5]

# Build a dataframe with the fitted data
poly_data = pd.DataFrame({'xfit': np.linspace(df['x'].min(), df['x'].max(), 500)})
for degree in degree_list:
    poly_data[str(degree)] = np.poly1d(np.polyfit(df['x'], df['y'], degree))(poly_data['xfit'])

# Plot the data points on an interactive axis
points = alt.Chart(df).mark_circle(color='black').encode(
    x=alt.X('x', title='x'),
    y=alt.Y('y', title='y')
).interactive()

# Plot the best fit polynomials
polynomial_fit = alt.Chart(poly_data).transform_fold(
    ['1', '3', '5'],
    as_=['degree', 'yfit']
).mark_line().encode(
    x='xfit:Q',
    y='yfit:Q',
    color='degree:N'
)

points + polynomial_fit
github altair-viz / altair / altair / vegalite / v1 / schema / core.py View on Github external
class AggregateOp(VegaLiteSchema):
    """AggregateOp schema wrapper

    enum('values', 'count', 'valid', 'missing', 'distinct', 'sum', 'mean', 'average',
    'variance', 'variancep', 'stdev', 'stdevp', 'median', 'q1', 'q3', 'modeskew', 'min', 'max',
    'argmin', 'argmax')
    """
    _schema = {'$ref': '#/definitions/AggregateOp'}
    _rootschema = Root._schema

    def __init__(self, *args):
        super(AggregateOp, self).__init__(*args)


class Type(VegaLiteSchema):
    """Type schema wrapper

    enum('quantitative', 'ordinal', 'temporal', 'nominal')
    """
    _schema = {'$ref': '#/definitions/Type'}
    _rootschema = Root._schema

    def __init__(self, *args):
        super(Type, self).__init__(*args)


class TimeUnit(VegaLiteSchema):
    """TimeUnit schema wrapper

    enum('year', 'month', 'day', 'date', 'hours', 'minutes', 'seconds', 'milliseconds',
    'yearmonth', 'yearmonthdate', 'yearmonthdatehours', 'yearmonthdatehoursminutes',
github altair-viz / altair_widgets / altair_widgets / widget.py View on Github external
def plot(self, show=True):
        """ Assumes nothing in self.settings is None (i.e., there are no keys
        in settings such that settings[key] == None"""

        kwargs = {e['encoding']: _get_plot_command(e)
                  for e in self.settings['encodings']}

        mark_opts = {k: v for k, v in self.settings['mark'].items()}
        mark = mark_opts.pop('mark')
        Chart_mark = getattr(altair.Chart(self.df), mark)
        self.chart = Chart_mark(**mark_opts).encode(**kwargs)
        if show and self.show:
            clear_output()
            display(self.chart)
github altair-viz / altair / altair / examples / poly_fit_regression.py View on Github external
# category: scatter plots

import numpy as np
import pandas as pd
import altair as alt

# Generate some random data
rng = np.random.RandomState(1)
x = rng.rand(40) ** 2
y = 10 - 1.0 / (x + 0.1) + rng.randn(40)
source = pd.DataFrame({"x": x, "y": y})

# Define the degree of the polynomial fits
degree_list = [1, 3, 5]

base = alt.Chart(source).mark_circle(color="black").encode(
        alt.X("x"), alt.Y("y")
)

polynomial_fit = [
    base.transform_regression(
        "x", "y", method="poly", order=order, as_=["x", str(order)]
    )
    .mark_line()
    .transform_fold([str(order)], as_=["degree", "y"])
    .encode(alt.Color("degree:N"))
    for order in degree_list
]

alt.layer(base, *polynomial_fit)
github great-expectations / great_expectations / great_expectations / render / renderer / column_section_renderer.py View on Github external
chart_pixel_width = (len(values) / 60.0) * 1000
            if chart_pixel_width < 200:
                chart_pixel_width = 200
            chart_container_col_width = round((len(values) / 60.0) * 12)
            if chart_container_col_width < 4:
                chart_container_col_width = 4
            elif chart_container_col_width > 8:
                chart_container_col_width = 12
            elif chart_container_col_width > 4:
                chart_container_col_width = 8

        mark_bar_args = {}
        if len(values) == 1:
            mark_bar_args["size"] = 20

        bars = alt.Chart(df).mark_bar(**mark_bar_args).encode(
            y='count:Q',
            x="value:O",
            tooltip=["value", "count"]
        ).properties(height=400, width=chart_pixel_width, autosize="fit")

        chart = bars.to_json()

        new_block = RenderedComponentContent(**{
            "content_block_type": "graph",
            "header":
                {
                    "template": "Value Counts",
                    "tooltip": {
                        "content": "expect_column_distinct_values_to_be_in_set"
                    }
                },
github piccolbo / altair_recipes / altair_recipes / stripplot.py View on Github external
def stripplot(
    data=None, columns=None, group_by=None, color=None, opacity=1, height=600, width=800
):
    """Generate a stripplot."""
    data, key, value = multivariate_preprocess(data, columns, group_by)
    enc_args = dict()
    if color is not None:
        enc_args["color"] = color
    return (
        alt.Chart(data, height=height, width=width)
        .mark_tick(opacity=opacity, thickness=2)
        .encode(x=key + ":N", y=value, **enc_args)
    )
github altair-viz / altair / altair / examples / co2_concentration.py View on Github external
"""
Atmospheric CO2 Concentration
-----------------------------
This example is a fully developed line chart that uses a window transformation.
It was inspired by `Gregor Aisch's work at datawrapper
`_.
"""
# category: case studies
import altair as alt
from vega_datasets import data

source = data.co2_concentration.url

base = alt.Chart(
    source,
    title="Carbon Dioxide in the Atmosphere"
).transform_calculate(
    year="year(datum.Date)"
).transform_calculate(
    decade="floor(datum.year / 10)"
).transform_calculate(
    scaled_date="(datum.year % 10) + (month(datum.Date)/12)"
).transform_window(
    first_date='first_value(scaled_date)',
    last_date='last_value(scaled_date)',
    sort=[{"field": "scaled_date", "order": "ascending"}],
    groupby=['decade'],
    frame=[None, None]
).transform_calculate(
  end="datum.first_date === datum.scaled_date ? 'first' : datum.last_date === datum.scaled_date ? 'last' : null"
github google / deepvariant / deepvariant / vcf_stats_vis.py View on Github external
def _placeholder_for_empty_chart(text_to_display,
                                 width=100,
                                 height=100,
                                 title=''):
  chart = alt.Chart({'values': [{'placeholder': text_to_display}]}) \
      .mark_text(size=14).encode(text='placeholder:N') \
      .properties(width=width, height=height, title=title)
  return chart