axvspan
在 Altair 中模仿 matplotlib 的最佳方法是rect
在 y 轴上使用与像素值相关的标记。
这是一个例子:
import altair as alt
import numpy as np
import pandas as pd
np.random.seed(1701)
data = pd.DataFrame({
'Voltage': np.linspace(0, 100, 10),
'Current': np.random.randn(10).cumsum()
})
cutoff = pd.DataFrame({
'start': [0, 8, 30],
'stop': [8, 30, 100]
})
line = alt.Chart(data).mark_line().encode(
x=alt.X('Voltage'),
y=alt.Y('Current')
)
areas = alt.Chart(
cutoff.reset_index()
).mark_rect(
opacity=0.2
).encode(
x='start',
x2='stop',
y=alt.value(0), # pixels from top
y2=alt.value(300), # pixels from top
color='index:N'
)
(areas + line).interactive()
![在此处输入图像描述](https://i.stack.imgur.com/mSPlc.png)