Reputation: 389
I've been working to create a chart similar to this EIA Chart (data at linked page):
I've seen a similar example using Altair in the 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 but I can't figure out how to pass in my own values. The mark_errorband examples make me believe that this must be possible however I am at a loss as to how to accomplish it.
I'd appreciate any guidance on how an min-max band may be achieved in Altair.
Upvotes: 8
Views: 3186
Reputation: 86330
You can use an area
mark with the y
and y2
encodings. For example:
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
Under the hood, mark_errorband
is essentially a macro within Vega-Lite that computes lower/upper bounds and automatically populates the y
and y2
encodings for you.
Upvotes: 12