Altair:创建一个 mark_line 图表,其最大最小带类似于 mark_errorband

Altair: Create a mark_line chart with a max-min band similar to mark_errorband

我一直在努力创建与此类似的图表 EIA Chart(链接页面上的数据):

我在 line chart with confidence interval band gallery example but I do not see a way to explicitly set the "extent" with my own values using the mark_errorband method. The documentation provides that you can use one of 4 methods to set the extent 中看到过使用 Altair 的类似示例,但我不知道如何传递我自己的值。 mark_errorband 示例让我相信这一定是可能的,但我不知道如何实现它。

对于如何在 Altair 中实现最小-最大范围的任何指导,我将不胜感激。

您可以将 area 标记与 yy2 编码一起使用。例如:

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

x = np.linspace(0, 10)
y = np.sin(x) + 0.1 * np.random.randn(len(x))

df = pd.DataFrame({
    'x': x,
    'y': y,
    'upper': y + 0.5 * (1 + np.random.rand(len(x))),
    'lower': y - 0.5 * (1 + np.random.rand(len(x)))
})

line = alt.Chart(df).mark_line(
    color='black'
).encode(
    x='x',
    y='y'
)

band = alt.Chart(df).mark_area(
    opacity=0.5, color='gray'
).encode(
    x='x',
    y='lower',
    y2='upper'
)

band + line

在幕后,mark_errorband 本质上是 Vega-Lite 中的一个宏,它计算 lower/upper 边界并自动为您填充 yy2 编码。