9

这就是我希望图像的样子

我试图在用 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',
)
4

1 回答 1

4

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

在此处输入图像描述

于 2020-12-14T14:57:55.977 回答