File: errorband.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 (171 lines) | stat: -rw-r--r-- 5,141 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
.. currentmodule:: altair

.. _user-guide-errorband-marks:

Error Band
~~~~~~~~~~

An error band summarizes an error range of quantitative values using a set of summary statistics, representing by area. Error band in Altair can either be used to aggregate raw data or directly visualize aggregated data.

To create an error band, use ``mark_errorband``.

Error Band Mark Properties
^^^^^^^^^^^^^^^^^^^^^^^^^^
An ``errorband`` mark definition can contain the following properties:

.. altair-object-table:: altair.ErrorBandDef
   :properties: extent orient color opacity interpolate tension

Besides the properties listed above, ``band`` and ``borders`` can be used to specify
the underlying mark properties for different parts of the error band as well.

Comparing the usage of Error Band to the usage of Error Bar
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
All the properties and usage of error band are identical to error bar’s, except the ``band`` and ``borders`` that replace the error bar’s ``rule`` and ``ticks``.

**Error Band**

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars.url

    alt.Chart(source).mark_errorband(extent="ci", borders=True).encode(
        x="year(Year)",
        y=alt.Y(
            "Miles_per_Gallon:Q",
            scale=alt.Scale(zero=False),
            title="Miles per Gallon (95% CIs)",
        ),
    )


**Error Bar**

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars.url

    alt.Chart(source).mark_errorbar(extent="ci", ticks=True).encode(
        x="year(Year)",
        y=alt.Y(
            "Miles_per_Gallon:Q",
            scale=alt.Scale(zero=False),
            title="Miles per Gallon (95% CIs)",
        ),
    )

Using Error Band to Aggregate Raw Data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If the data is not aggregated yet, Altair will aggregate the data based on the ``extent`` properties in the mark definition as done in the error band showing confidence interval above. All other ``extent`` values are defined in Error Bar.

Using Error Band to Visualize Aggregated Data
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1. Data is aggregated with low and high values of the error band
If the data is already pre-aggregated with low and high values of the error band, you can directly specify ``x`` and ``x2`` (or ``y`` and ``y2``) to use error band as a ranged mark.

.. altair-plot::
    import altair as alt
    import pandas as pd

    source = pd.DataFrame(
        {
            "ci1": [23.5007, 25.8214, 26.4472, 27.7074],
            "ci0": [19.6912, 20.8554, 21.9749, 22.6203],
            "center": [21.5735, 23.3750, 24.0611, 25.0931],
            "Year": [189302400000, 220924800000, 252460800000, 283996800000],
        }
    )

    band = alt.Chart(source).mark_errorband().encode(
        alt.Y(
            "ci1:Q",
            scale=alt.Scale(zero=False),
            title="Mean of Miles per Gallon (95% CIs)"
        ),
        alt.Y2("ci0:Q"),
        alt.X("year(Year)"),
    )

    line = alt.Chart(source).mark_line().encode(
        alt.Y("center:Q"),
        alt.X("year(Year)")
    )

    band + line

2. Data is aggregated with center and error value(s)
If the data is already pre-aggregated with center and error values of the error band, you can use ``x/y``, ``x/yError``, and ``x/yError2`` as defined in Error Bar.

Dimension
^^^^^^^^^

Altair supports both 1D and 2D error bands:

A **1D error band** shows the error range of a continuous field; it can be used to show the global error range of the whole plot.

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars.url

    band = alt.Chart(source).mark_errorband(extent="stdev").encode(
        alt.Y("Miles_per_Gallon:Q").title("Miles per Gallon")
    )

    points = alt.Chart(source).mark_point().encode(
        x="Horsepower:Q",
        y="Miles_per_Gallon:Q",
    )

    band + points


A **2D error** band shows the error range of a continuous field for each dimension value such as year.

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars()

    line = alt.Chart(source).mark_line().encode(
        x="Year",
        y="mean(Miles_per_Gallon)"
    )

    band = alt.Chart(source).mark_errorband(extent="ci").encode(
        x="Year",
        y=alt.Y("Miles_per_Gallon").title("Miles/Gallon"),
    )

    band + line

Color and Opacity Encoding Channels
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can customize the color and opacity of the bands by using the ``color`` and ``opacity`` encoding channels.

Here is an example of a ``errorband`` with the ``color`` encoding channel set to ``alt.value('black')``.

.. altair-plot::
    import altair as alt
    from vega_datasets import data

    source = data.cars.url

    alt.Chart(source).mark_errorband(extent="ci", borders=True).encode(
        x="year(Year)",
        y=alt.Y("Miles_per_Gallon:Q")
            .scale(zero=False)
            .title("Miles per Gallon (95% CIs)"),
        color=alt.value("black")
    )