如何用Altair遮蔽一个区域

时间:2017-04-18 20:56:57

标签: python region altair

This is how I would like the image to look like

我试图在用Altair创建的情节中遮蔽不同的区域(如matplotlib中的axvspan),但无法找到方法。

Chart(data).mark_line(color='r').encode(
    x=X('Voltage'),
    y=Y('Current (pA)', axis=Axis(format='r'), title='Current (pA)'),
    color='Line polarity:N',
    shape='Line polarity:N',
)

1 个答案:

答案 0 :(得分:0)

在Altair中模拟matplotlib的axvspan的最好方法是在{y}轴上绑定一个rect标记。

这里是一个例子:

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

enter image description here