用 altair 填充两行

fill between 2 lines with altair

我想在 Altair 中突出显示两条线之间的区域。

我试着看看是否 解决方案是使用 mark_area() 但我找不到如何用数据指定下限。

我的数据(几行):

index   created     pct_pl_transit  pct_pl_transit_max
0   1970-01-01 00:00:00     49.627697   60.056873
2   1970-01-01 01:00:00     43.800967   55.301460
4   1970-01-01 02:00:00     41.440480   49.757740
6   1970-01-01 03:00:00     37.879753   40.352226
8   1970-01-01 04:00:00     36.691287   19.429075

我的图表:

base=alt.Chart(lien_traf_gest_traf_lapi).encode(x=alt.X('created', axis=alt.Axis(title='Heure', format='%Hh%M')))
line_pct_pl_lapi=base.mark_line(color='blue').encode(y=alt.Y('pct_pl_transit:Q', axis=alt.Axis(title='Nombre de PL SIREDO')))
line_pct_max=base.mark_line().encode(y='pct_pl_transit_max')
line_pct_max+line_pct_pl_lapi

您可以使用 yy2 编码以及 mark_area() 来填充两个值之间的区域。例如:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': range(5),
    'ymin': [1, 3, 2, 4, 5],
    'ymax': [2, 1, 3, 6, 4]
})

alt.Chart(df).mark_area().encode(
    x='x:Q',
    y='ymin:Q',
    y2='ymax:Q'
)