pythonseabornseaborn-objects

How to create line + ribbon plot with seaborn.objects?


I would like to create a line + ribbon plot similar to these ones created with ggplot2 using geom_line + geom_ribbon:

line and ribbon plot

For that, I would like to use the new seaborn.objects interface, but even though the Plot function does accept ymin and ymax, I don't know how to use them effectively. I'm trying out several combinations of .add(so.Line()).add(so.Area()) but getting various errors or not the result I want.

Sample data:

import numpy as np
import polars as pl

pl.DataFrame({"value": np.random.randn(10), "std": 0.2 * np.abs(np.random.randn(10))})

Example output using matplotlib Axes.fill_between:

enter image description here


Solution

  • You want seaborn.objects.Band:

    import numpy as np
    import pandas as pd
    import seaborn.objects as so
    
    df = pd.DataFrame({"value": np.random.randn(10), "std": 0.2 * np.abs(np.random.randn(10))})
    
    (
        so.Plot(
            x=df.index,
            y=df["value"],
            ymin=df.eval("value - std"),
            ymax=df.eval("value + std")
        )
        .add(so.Line())
        .add(so.Band())
    )
    

    enter image description here