Python/altair

[Altair] Error Band

Unlimited Jun 2024. 9. 22. 09:19

작성일자 : 2024-09-22

Ver 0.1.1

 

Error Band

Error Band는 면적별 요약 통계 세트를 사용하여 정량적 값의 오차 범위를 요약한다.

Altair의 Error Band는 Raw 데이터를 집계하거나 집계된 데이터를 직접 시각화하는 데 사용할 수 있다.

Error Band를 만들려면 mark_errorband를 사용합니다.


속성

A circle mark definition can contain any standard mark properties and the following special properties:

속성 유형 설명
extent ErrorBarExtent The extent of the band. Available options include:
  • "ci": Extend the band to the confidence interval of the mean.
  • "stderr": The size of band are set to the value of standard error, extending from the mean.
  • "stdev": The size of band are set to the value of standard deviation, extending from the mean.
  • "iqr": Extend the band to the q1 and q3.
Default value: "stderr".
orient Orientation Orientation of the error band. This is normally automatically determined, but can be specified when the orientation is ambiguous and cannot be automatically determined.
color anyOf(Color, Gradient, ExprRef) Default color.
Default value: "#4682b4"
Note:
  • This property cannot be used in a style config.
  • The fill and stroke properties have higher precedence than color and will override color.
opacity number The opacity (value between [0,1]) of the mark.
interpolate Interpolate The line interpolation method for the error band. One of the following:
  • "linear": piecewise linear segments, as in a polyline.
  • "linear-closed": close the linear segments to form a polygon.
  • "step": a piecewise constant function (a step function) consisting of alternating horizontal and vertical lines. The y-value changes at the midpoint of each pair of adjacent x-values.
  • "step-before": a piecewise constant function (a step function) consisting of alternating horizontal and vertical lines. The y-value changes before the x-value.
  • "step-after": a piecewise constant function (a step function) consisting of alternating horizontal and vertical lines. The y-value changes after the x-value.
  • "basis": a B-spline, with control point duplication on the ends.
  • "basis-open": an open B-spline; may not intersect the start or end.
  • "basis-closed": a closed B-spline, as in a loop.
  • "cardinal": a Cardinal spline, with control point duplication on the ends.
  • "cardinal-open": an open Cardinal spline; may not intersect the start or end, but will intersect other control points.
  • "cardinal-closed": a closed Cardinal spline, as in a loop.
  • "bundle": equivalent to basis, except the tension parameter is used to straighten the spline.
  • "monotone": cubic interpolation that preserves monotonicity in y.
tension number The tension parameter for the interpolation type of the error band

 


Comparing the usage of Error Band to the usage of Error Bar

Error Band 의 모든 속성과 사용 방식은 Error Bar의 규칙과 틱을 대체하는 밴드와 테두리를 제외하고는 Error Bar와 동일하다.

Error Band

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

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

아직 데이터가 집계되지 않은 경우, Altair는 위의 신뢰 구간을 나타내는 Error Band에서 수행한 것처럼 마크 정의의 범위 속성을 기반으로 데이터를 집계한다. 다른 모든 범위 값은 오류 표시줄에 정의되어 있다.


Using Error Band to Visualize Aggregated Data

1. 데이터는 Error Band의 낮은 값과 높은 값으로 집계된다. 데이터가 이미 Error Band의 낮은 값과 높은 값으로 사전 집계된 경우, Error Band를 범위 표시로 사용하려면 x와 x2(또는 y와 y2)를 직접 지정할 수 있다.

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. 데이터는 중심 및 오류 값으로 집계된다. 데이터가 이미 Error Band의 중심 및 오류 값으로 사전 집계된 경우 오류 표시줄에 정의된 대로 x/y, x/yError 및 x/yError2를 사용할 수 있습니다.


Dimension

Altair는 1D 및 2D Error Band 모두 지원합니다:
1D Error Band는 연속 필드의 오차 범위를 표시하며, 전체 플롯의 전역 오차 범위를 표시하는 데 사용할 수 있습니다.

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

2D Error Band는 연도와 같은 각 차원 값에 대한 연속 필드의 오차 범위를 보여줍니다.

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

색상 및 불투명도 인코딩 채널을 사용하여 밴드의 색상과 불투명도를 사용자 지정할 수 있다. 아래 코드는 색상 인코딩 채널이 alt.value('black')로 설정된 Error Band의 예입니다.

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")
)