【问题标题】:Line Chart with Custom Confidence Interval in AltairAltair 中具有自定义置信区间的折线图
【发布时间】:2020-03-12 07:37:09
【问题描述】:

假设我有下面的数据框:

我检查了documentation,但它仅基于单个列。

可重现的代码:

x = np.random.normal(100,5,100)
data = pd.DataFrame(x)
epsilon = 10
data.columns = ['x']
data['lower'] = x - epsilon
data['upper'] = x + epsilon
data

我其实更喜欢使用 altair,因为我喜欢它的交互性。

【问题讨论】:

标签: python-3.x machine-learning data-science altair


【解决方案1】:

您可以将折线图和面积图分层,使用yy2 编码来指定范围:

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

x = np.random.normal(100,5,100)
epsilon = 10
data = pd.DataFrame({
    'x': x,
    'lower': x - epsilon,
    'upper': x + epsilon
}).reset_index()

line = alt.Chart(data).mark_line().encode(
    x='index',
    y='x'
)

band = alt.Chart(data).mark_area(
    opacity=0.5
).encode(
    x='index',
    y='lower',
    y2='upper'
)

band + line

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多