File: stack.rst

package info (click to toggle)
python-altair 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,952 kB
  • sloc: python: 25,649; sh: 14; makefile: 5
file content (57 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
.. currentmodule:: altair

.. _user-guide-stack-transform:

Stack
~~~~~
The stack transform allows you to compute values associated with stacked versions
of encodings. For example, consider this stacked bar chart:

.. altair-plot::

    import altair as alt
    from vega_datasets import data

    source = data.barley()

    alt.Chart(source).mark_bar().encode(
        column='year:O',
        x='yield:Q',
        y='variety:N',
        color='site:N'
    ).properties(width=220)

Implicitly, this data is being grouped and stacked, but what if you would like to
access those stacked values directly?
We can construct that same chart manually using the stack transform:

.. altair-plot::

    import altair as alt
    from vega_datasets import data

    source = data.barley()

    alt.Chart(source).transform_stack(
        stack='yield',
        as_=['yield_1', 'yield_2'],
        groupby=['year', 'variety'],
        sort=[alt.SortField('site', 'descending')]
    ).mark_bar().encode(
        column='year:O',
        x=alt.X('yield_1:Q').title('yield'),
        x2='yield_2:Q',
        y='variety:N',
        color='site:N',
        tooltip=['site', 'yield', 'variety']
    ).properties(width=220)

Notice that the bars are now explicitly drawn between values computed and
specified within the x and x2 encodings.

Transform Options
^^^^^^^^^^^^^^^^^
The :meth:`~Chart.transform_stack` method is built on the :class:`~StackTransform`
class, which has the following options:

.. altair-object-table:: altair.StackTransform