How to plot an error band with weighted data

Viewed 29

Seaborn is a beautiful package that let's me plot data from DataFrames, with automatic error band.

Seaborn calculates the average and the deviation when the dataframe holds multiple y values for an x value. But what if the data in my DataFrame is weighted? Consider this example:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

data = {
    'x' : [1, 2, 3, 4]*3,   # 3 sets of same x values
    'y' : [1.5, 2.1, 3.1, 4.1, 0.9, 1.9, 2.9, 3.9, 1, 2, 3, 4], # 3 sets of slightly different y values
    'w' : [100] + [0.1]*11   # weights
}

data = pd.DataFrame(data)

sns.lineplot(data, x='x', y='y')
plt.show()

Here seaborn will take the unweighted values for x and y. Is there a neat way to take the column 'w' into account when taking the average?

I've taken a look at estimator argument of lineplot, but a function passed under this argument does not appear to have access to the weight column.

Any suggestions are appreciated!

0 Answers
Related