首页 > 解决方案 > 用 altair 在 2 行之间填充

问题描述

我想突出显示 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

标签: pythonaltair

解决方案


您可以使用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'
)

在此处输入图像描述


推荐阅读