Yit*_*iti 8 python region altair
我试图在我用 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',
)
Run Code Online (Sandbox Code Playgroud)
在 Altair 中模仿 matplotlib 的最佳方法axvspan是使用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()
Run Code Online (Sandbox Code Playgroud)