Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fill between 2 lines with altair

Tags:

python

altair

I want to highlight the area between 2 lines in Altair. results : lines filled

I tried to look if the solution is using mark_area() but I can't find how to specify the low limit with data.

my data (few lines) :

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

my chart :

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
like image 827
nantodevison Avatar asked Jun 20 '19 09:06

nantodevison


1 Answers

You can use the y and y2 encodings with mark_area() to fill the area between the two values. For example:

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

enter image description here

like image 58
jakevdp Avatar answered Oct 24 '22 05:10

jakevdp