Python altair.Chart() Examples

The following are 30 code examples of altair.Chart(). 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: BubbleDiachronicVisualization.py    From scattertext with Apache License 2.0 8 votes vote down vote up
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),
        ) 
Example #2
Source File: widget.py    From altair_widgets with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def plot(self, show=True):
        kwargs = {
            e["encoding"]: _get_plot_command(e) for e in self.settings["encodings"]
        }
        kwargs = {k: v for k, v in kwargs.items() if v is not None}

        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("Updating...")
            with io.StringIO() as f:
                self.chart.save(f, format="svg")
                f.seek(0)
                html = f.read()
            clear_output()
            display(self.controller)
            display(SVG(html)) 
Example #3
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def stripplot(x=None, y=None, hue=None, data=None):
    # TODO: refactor so it uses category_chart_kwargs()
    if data is None:
        if y is None:
            data = x.to_frame()
            x = data.columns[0]
        elif x is None:
            data = y.to_frame()
            y = data.columns[0]
        else:
            raise RuntimeError('not supported yet ...')

    kwargs = {}
    if x is not None:
        kwargs['x'] = '{x}'.format(x=x)
    if y is not None:
        kwargs['y'] = '{y}'.format(y=y)
    if hue is not None:
        kwargs['color'] = hue

    chart = alt.Chart(data).mark_tick().encode(**kwargs)
    return chart 
Example #4
Source File: plot.py    From retentioneering-tools with Mozilla Public License 2.0 6 votes vote down vote up
def altair_cluster_tsne(data, clusters, target, plot_name=None, **kwargs):
    if hasattr(data.retention, '_tsne'):
        tsne = data.retention._tsne.copy()
    else:
        tsne = data.retention.learn_tsne(clusters, **kwargs)
    tsne['color'] = clusters
    tsne.columns = ['x', 'y', 'color']

    scatter = alt.Chart(tsne).mark_point().encode(
        x='x',
        y='y',
        color=alt.Color(
            'color',
            scale=alt.Scale(scheme='plasma')
        )
    ).properties(
        width=800,
        height=600
    )
    return scatter, plot_name, tsne, data.retention.retention_config 
Example #5
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 #6
Source File: plot.py    From retentioneering-tools with Mozilla Public License 2.0 6 votes vote down vote up
def altair_cluster_bar(data, clusters, target, plot_name=None, plot_cnt=None, metrics=None, **kwargs):
    cl = pd.DataFrame([clusters, target], index=['clusters', 'target']).T
    cl['cnt'] = 1
    cl.target = cl.target.astype(int)
    bars = cl.groupby('clusters').agg({
        'cnt': 'sum',
        'target': 'mean'
    }).reset_index()
    bars.cnt /= bars.cnt.sum()
    bars = bars.loc[:, ['clusters', 'cnt']].append(bars.loc[:, ['clusters', 'target']], ignore_index=True, sort=False)
    bars['target'] = np.where(bars.target.isnull(), bars.cnt, bars.target)
    bars['Metric'] = np.where(bars['cnt'].isnull(), 'Average CR', 'Cluster size')
    # print(bars, type(bars))
    bar = alt.Chart(bars).mark_bar().encode(
        x='Metric:O',
        y='target:Q',
        color='Metric:N',
        column='clusters:N'
    ).properties(
        width=60,
        height=200
    )

    return bar, plot_name, None, data.retention.retention_config 
Example #7
Source File: runways.py    From traffic with MIT License 6 votes vote down vote up
def geoencode(
        self, mode: str = "geometry"
    ) -> Optional[alt.Chart]:  # coverage: ignore

        if mode == "geometry":
            return (
                super().geoencode().mark_geoshape(strokeWidth=2, stroke="black")
            )

        elif mode == "labels":
            rwy_labels = alt.Chart(self.data).encode(
                longitude="longitude:Q", latitude="latitude:Q", text="name:N"
            )
            rwy_layers = [
                rwy_labels.transform_filter(alt.datum.name == name).mark_text(
                    angle=bearing, baseline="middle", dy=10
                )
                for (name, bearing) in zip(self.data.name, self.data.bearing)
            ]

            return alt.layer(*rwy_layers)

        else:
            return None 
Example #8
Source File: interface.py    From timesketch with Apache License 2.0 6 votes vote down vote up
def _get_chart_with_transform(self):
        """Returns a chart object potentially with a URL transform added to it.

        Returns:
            A LayerChart object, either with added transform or not, depending
            on whether sketch URL and field are set.
        """
        chart = alt.Chart(self.values)
        if not self._sketch_url:
            return chart

        if not self._field:
            return chart

        datum = getattr(alt.datum, self._field)
        if self._aggregation_id:
            agg_string = 'a={0:d}&'.format(self._aggregation_id)
        else:
            agg_string = ''
        url = '{0:s}?{1:s}q={2:s}:"'.format(
            self._sketch_url, agg_string, self._field)
        return chart.transform_calculate(
            url=url + datum + '" ' + self._extra_query_url) 
Example #9
Source File: _core.py    From altair_pandas with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def hist(self, bins=None, orientation="vertical", **kwargs):
        data = self._preprocess_data(with_index=False)
        column = data.columns[0]
        if isinstance(bins, int):
            bins = alt.Bin(maxbins=bins)
        elif bins is None:
            bins = True
        if orientation == "vertical":
            Indep, Dep = alt.X, alt.Y
        elif orientation == "horizontal":
            Indep, Dep = alt.Y, alt.X
        else:
            raise ValueError("orientation must be 'horizontal' or 'vertical'.")

        mark = self._get_mark_def({"type": "bar", "orient": orientation}, kwargs)
        return alt.Chart(data, mark=mark).encode(
            Indep(column, title=None, bin=bins), Dep("count()", title="Frequency")
        ) 
Example #10
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def violinplot(x=None, y=None, data=None, orient=None):
    # TODO: automatically infer orientation

    if orient is None or orient == 'v':
        kwargs = dict(
                    x=alt.X('count(*):Q',
                            axis=alt.Axis(grid=False, labels=False),
                            stack='center',
                            title=''),
                    y=alt.Y('{y}:Q'.format(y=y), bin=alt.Bin(maxbins=100)),
                    column='{x}:N'.format(x=x),
                    color='{x}:N'.format(x=x)
        )
    else:
        kwargs = dict(
                    y=alt.Y('count(*):Q',
                            axis=alt.Axis(grid=False, labels=False),
                            stack='center',
                            title=''),
                    x=alt.X('{x}:Q'.format(x=x), bin=alt.Bin(maxbins=100)),
                    row='{y}:N'.format(y=y),
                    color='{y}:N'.format(y=y)
        )
    chart = alt.Chart(data).mark_area().encode(**kwargs)
    return chart 
Example #11
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def pairplot(data, hue=None, vars=None):
    if vars is None:
        vars = list(data.columns)

    chart = alt.Chart(data).mark_circle().encode(
                alt.X(alt.repeat("column"), type='quantitative'),
                alt.Y(alt.repeat("row"), type='quantitative'),
                color='{hue}:N'.format(hue=hue)
            ).properties(
                width=250,
                height=250
            ).repeat(
                row=vars,
                column=vars
            )
    return chart 
Example #12
Source File: core.py    From nx_altair with MIT License 6 votes vote down vote up
def to_chart(G, pos):
    """Construct a single Altair Chart for
    """
    # Build node layer
    node_df = to_pandas_nodes(G, pos)
    node_layer = alt.Chart(node_df)

    # Build edge layer
    edge_df = to_pandas_edges(G, pos)
    edge_layer = alt.Chart(edge_df)

    # Layer chart
    chart = alt.LayerChart(
        layer=(edge_layer, node_layer)
    )
    chart = despine(chart)
    return chart 
Example #13
Source File: aggregation.py    From timesketch with Apache License 2.0 6 votes vote down vote up
def generate_chart(self):
        """Returns an altair Vega-lite chart."""
        if not self.chart_type:
            raise TypeError('Unable to generate chart, missing a chart type.')

        if not self._parameters.get('supported_charts'):
            self._parameters['supported_charts'] = self.chart_type

        data = self.lazyload_data()
        meta = data.get('meta', {})
        vega_spec = meta.get('vega_spec')

        if not vega_spec:
            return altair.Chart(pandas.DataFrame()).mark_point()

        vega_spec_string = json.dumps(vega_spec)
        return altair.Chart.from_json(vega_spec_string) 
Example #14
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def scatterplot(x, y, data, hue=None, xlim=None, ylim=None):
    # TODO: refactor so it uses category_chart_kwargs?
    if xlim is None:
        xlim = get_limit_tuple(data[x])
    if ylim is None:
        ylim = get_limit_tuple(data[y])
    xscale = alt.Scale(domain=xlim)
    yscale = alt.Scale(domain=ylim)
    
    other_args = {'color': '{hue}:N'.format(hue=hue)} if hue else {}
    points = alt.Chart(data).mark_circle().encode(
        alt.X(x, scale=xscale),
        alt.Y(y, scale=yscale),
        **other_args
    )
    return points 
Example #15
Source File: structure.py    From traffic with MIT License 6 votes vote down vote up
def geoencode(
        self,
        footprint: bool = True,
        runways: bool = False,
        labels: bool = False,
    ) -> alt.Chart:  # coverage: ignore
        cumul = []
        if footprint:
            cumul.append(super().geoencode())
        if runways:
            cumul.append(self.runways.geoencode())
        if labels:
            cumul.append(self.runways.geoencode("labels"))
        if len(cumul) == 0:
            raise TypeError(
                "At least one of footprint, runways and labels must be True"
            )
        return alt.layer(*cumul) 
Example #16
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 
Example #17
Source File: test_geodata.py    From gpdvega with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_geojson_feature():

    def Chart(data, **arg):
        return alt.Chart(
                        gpdvega.geojson_feature(data, 'test_prop')
                ).mark_geoshape().project().encode(**arg)
    # Fake GeoInterface
    data = _create_fake_geo_interface()
    with alt.data_transformers.enable(consolidate_datasets=False):
        dct = Chart(data).to_dict()

    assert dct['data']['format'] == {'type': 'json', 'property': 'test_prop'}
    assert dct['data']['values'] == data.__geo_interface__

    # url
    data = "url.json"
    with alt.data_transformers.enable(consolidate_datasets=False):
        dct = Chart(data).to_dict()

    assert dct['data']['format'] == {'type': 'json', 'property': 'test_prop'}
    assert dct['data']['url'] == data

    # GeoPandas

    data = geojson2gdp(_create_geojson())
    with alt.data_transformers.enable(consolidate_datasets=False):
        dct = Chart(data).to_dict()

    assert dct['data']['format'] == {'type': 'json', 'property': 'test_prop'}

    assert json.dumps(dct['data']['values'], sort_keys=True) == \
        json.dumps(data.__geo_interface__, sort_keys=True)

    # GeoPandas sanitize_dataframe
    data['time'] = pd.date_range('2018-01-01', periods=data.index.size)
    with alt.data_transformers.enable(consolidate_datasets=False):
        dct = Chart(data).to_dict() 
Example #18
Source File: _core.py    From altair_pandas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def box(self, vert=True, **kwargs):
        data = self._preprocess_data(with_index=False)
        chart = (
            alt.Chart(data)
            .transform_fold(list(data.columns), as_=["column", "value"])
            .mark_boxplot()
            .encode(x=alt.X("column:N", title=None), y="value:Q")
        )
        if not vert:
            chart.encoding.x, chart.encoding.y = chart.encoding.y, chart.encoding.x
        return chart 
Example #19
Source File: _core.py    From altair_pandas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def hist_frame(self, column=None, layout=(-1, 2), **kwargs):
        if column is not None:
            if isinstance(column, str):
                column = [column]
        data = self._preprocess_data(with_index=False, usecols=column)
        data = data._get_numeric_data()
        nrows, ncols = _get_layout(data.shape[1], layout)
        return (
            alt.Chart(data, mark=self._get_mark_def("bar", kwargs))
            .encode(
                x=alt.X(alt.repeat("repeat"), type="quantitative", bin=True),
                y=alt.Y("count()", title="Frequency"),
            )
            .repeat(repeat=list(data.columns), columns=ncols)
        ) 
Example #20
Source File: core.py    From starborn with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def jointplot(x, y, data, kind='scatter', hue=None, xlim=None, ylim=None):
    if xlim is None:
        xlim = get_limit_tuple(data[x])
    if ylim is None:
        ylim = get_limit_tuple(data[y])
    xscale = alt.Scale(domain=xlim)
    yscale = alt.Scale(domain=ylim)
 
    points = scatterplot(x, y, data, hue=hue, xlim=xlim, ylim=ylim)

    area_args = {'opacity': .3, 'interpolate': 'step'}

    blank_axis = alt.Axis(title='')

    top_hist = alt.Chart(data).mark_area(**area_args).encode(
        alt.X('{x}:Q'.format(x=x),
              # when using bins, the axis scale is set through
              # the bin extent, so we do not specify the scale here
              # (which would be ignored anyway)
              bin=alt.Bin(maxbins=20, extent=xscale.domain),
              stack=None,
              axis=blank_axis,
             ),
        alt.Y('count()', stack=None, axis=blank_axis),
        alt.Color('{hue}:N'.format(hue=hue)),
    ).properties(height=60)

    right_hist = alt.Chart(data).mark_area(**area_args).encode(
        alt.Y('{y}:Q'.format(y=y),
              bin=alt.Bin(maxbins=20, extent=yscale.domain),
              stack=None,
              axis=blank_axis,
             ),
        alt.X('count()', stack=None, axis=blank_axis),
        alt.Color('{hue}:N'.format(hue=hue)),
    ).properties(width=60)

    return top_hist & (points | right_hist) 
Example #21
Source File: driver.py    From altair-transform with MIT License 5 votes vote down vote up
def apply(
    df: pd.DataFrame,
    transform: Union[
        None, JSONDict, alt.Transform, List[Union[JSONDict, alt.Transform]]
    ] = None,
) -> pd.DataFrame:
    """Extract transformed data from a Javascript rendering.

    Parameters
    ----------
    df : pd.DataFrame
    transform : list|dict
        A transform specification or list of transform specifications.
        Each specification must be valid according to Altair's transform
        schema.

    Returns
    -------
    df_transformed : pd.DataFrame
        The transformed dataframe.
    """
    if transform is None:
        transform = []
    elif not isinstance(transform, list):
        transform = [transform]
    chart = alt.Chart(df).mark_point()._add_transform(*transform)
    with alt.data_transformers.enable(max_rows=None, consolidate_datasets=False):
        spec = chart.to_dict()
    return _extract_data(spec, "data_0") 
Example #22
Source File: test_driver.py    From altair-transform with MIT License 5 votes vote down vote up
def test_extract_data_source(driver):
    df = pd.DataFrame({"x": [1, 2, 3], "y": ["A", "B", "C"]})
    chart = alt.Chart(df).mark_point()
    with alt.data_transformers.enable(consolidate_datasets=False):
        spec = chart.to_dict()
    df_out = driver._extract_data(spec, "source_0")
    assert_frame_equal(df, df_out) 
Example #23
Source File: test_core.py    From altair-transform with MIT License 5 votes vote down vote up
def test_transform_chart_with_aggregate():
    data = pd.DataFrame({"x": list("AABBBCCCC")})
    chart = alt.Chart(data).mark_bar().encode(x="x:N", y="count():Q")
    chart_out = transform_chart(chart)
    assert chart_out.data.equals(pd.DataFrame({"x": list("ABC"), "__count": [2, 3, 4]}))
    assert chart_out.encoding.to_dict() == {
        "x": {"field": "x", "type": "nominal"},
        "y": {"field": "__count", "type": "quantitative", "title": "Count of Records"},
    } 
Example #24
Source File: _core.py    From altair_pandas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _xy(self, mark, **kwargs):
        data = self._preprocess_data(with_index=True)
        return (
            alt.Chart(data, mark=self._get_mark_def(mark, kwargs))
            .encode(
                x=alt.X(data.columns[0], title=None),
                y=alt.Y(data.columns[1], title=None),
                tooltip=list(data.columns),
            )
            .interactive()
        ) 
Example #25
Source File: core.py    From altair-transform with MIT License 5 votes vote down vote up
def extract_data(
    chart: alt.Chart, apply_encoding_transforms: bool = True
) -> pd.DataFrame:
    """Extract transformed data from a chart.

    This only works with data and transform defined at the
    top level of the chart.

    Parameters
    ----------
    chart : alt.Chart
        The chart instance from which the data and transform
        will be extracted
    apply_encoding_transforms : bool
        If True (default), then apply transforms specified within an
        encoding as well as those specified directly in the transforms
        attribute.

    Returns
    -------
    df_transformed : pd.DataFrame
        The extracted and transformed dataframe.

    Example
    -------
    >>> import pandas as pd
    >>> data = pd.DataFrame({'x': range(5), 'y': list('ABCAB')})
    >>> chart = alt.Chart(data).mark_bar().encode(x='sum(x)', y='y')
    >>> extract_data(chart)
       y  sum_x
    0  A      3
    1  B      5
    2  C      2
    """
    if apply_encoding_transforms:
        chart = extract_transform(chart)
    return apply(to_dataframe(chart.data, chart), chart.transform) 
Example #26
Source File: core.py    From altair-transform with MIT License 5 votes vote down vote up
def apply(
    df: pd.DataFrame,
    transform: Union[alt.Transform, List[alt.Transform]],
    inplace: bool = False,
) -> pd.DataFrame:
    """Apply transform or transforms to dataframe.

    Parameters
    ----------
    df : pd.DataFrame
    transform : list|dict
        A transform specification or list of transform specifications.
        Each specification must be valid according to Altair's transform
        schema.
    inplace : bool
        If True, then dataframe may be modified in-place. Default: False.

    Returns
    -------
    df_transformed : pd.DataFrame
        The transformed dataframe.

    Example
    -------
    >>> import pandas as pd
    >>> data = pd.DataFrame({'x': range(5), 'y': list('ABCAB')})
    >>> chart = alt.Chart(data).transform_aggregate(sum_x='sum(x)', groupby=['y'])
    >>> apply(data, chart.transform)
       y  sum_x
    0  A      3
    1  B      5
    2  C      2
    """
    if not inplace:
        df = df.copy()
    if transform is alt.Undefined:
        return df
    return visit(transform, df) 
Example #27
Source File: plotting.py    From pdvega with MIT License 5 votes vote down vote up
def lag_plot(data, lag=1, kind="scatter", **kwds):
    """Lag plot for time series.

    Parameters
    ----------
    data: pandas.Series
        the time series to plot
    lag: integer
        The lag of the scatter plot, default=1
    kind: string
        The kind of plot to use (e.g. 'scatter', 'line')
    **kwds:
        Additional keywords passed to data.vgplot.scatter

    Returns
    -------
    chart: alt.Chart object
    """
    if lag != int(lag) or int(lag) <= 0:
        raise ValueError("lag must be a positive integer")
    lag = int(lag)

    values = data.values
    y1 = "y(t)"
    y2 = "y(t + {0})".format(lag)
    lags = pd.DataFrame({y1: values[:-lag].T.ravel(), y2: values[lag:].T.ravel()})

    if isinstance(data, pd.DataFrame):
        lags["variable"] = np.repeat(data.columns, lags.shape[0] / data.shape[1])
        kwds["c"] = "variable"

    return lags.vgplot(kind=kind, x=y1, y=y2, **kwds) 
Example #28
Source File: test_data.py    From altair-transform with MIT License 5 votes vote down vote up
def chart(named_data, inline_data):
    return alt.Chart(
        data=named_data,
        mark="bar",
        datasets={named_data["name"]: inline_data["values"]},
    ) 
Example #29
Source File: _core.py    From pdvega with MIT License 5 votes vote down vote up
def _plot(self, data=None, width=450, height=300, title=None, figsize=None, dpi=75):
        if data is None:
            data = self._data

        if title is None:
            title = ""

        if figsize is not None:
            width_inches, height_inches = figsize
            width = 0.8 * dpi * width_inches
            height = 0.8 * dpi * height_inches

        chart = alt.Chart(data=data).properties(width=width, height=height, title=title)
        return chart 
Example #30
Source File: aggregation.py    From timesketch with Apache License 2.0 5 votes vote down vote up
def get_charts(self):
        """Returns a list of altair Chart objects from each aggregation."""
        return [x.chart for x in self._aggregations]