-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Hi, I would like to use a weighted mean estimator for calculating confidence intervals on various seaborn plots. In the past I have done this via a 'hack' suggested here which uses complex numbers to encode the data and its weights before passing to a seaborn plotting function.
Unfortunately, as of upgrading to seaborn v0.13.0, this approach no longer works as it seems like the complex numbers are cast to reals at some point in the plotting process (and hence lose part of the data). This had previously worked up until v0.12.2.
I appreciate this was always a bit of a hack, but would either of the following be possible:
a) Add native support for weighted mean estimators to the seaborn plotting functions or,
b) Restore this hacky behaviour for now in a future release
I have tried alternatives such as storing the data and its weights in tuples or dataclasses, however neither of these approaches work as the data types are not numeric.
Language and package versions:
- Python v3.11.5
- numpy v1.26.2
- matplotlib v3.8.1
- pandas v2.1.3
Example code:
import pandas as pd
import seaborn as sns
import numpy as np
randomGenerator = np.random.default_rng(123)
values = randomGenerator.normal(10, 5, size=(100,))
weights = randomGenerator.uniform(size=(100,))
dataFrame = pd.DataFrame({'values': values, 'weights': weights})
dataFrame['valuesWithWeights'] = dataFrame['values'] + 1j * dataFrame['weights']
def WeightedMean(valuesWithWeights, **kwargs):
values, weights = np.real(valuesWithWeights), np.imag(valuesWithWeights)
weightedSum = np.sum((weights * values)) / np.sum(weights)
return weightedSum
sns.barplot(data=dataFrame, y='valuesWithWeights', estimator=WeightedMean)
Output using seaborn v0.12.2
Output using seaborn v0.13.0
[c:\Temp\seaborn_test\seaborn-venv\Lib\site-packages\matplotlib\cbook.py:1699](file:///C:/Temp/seaborn_test/seaborn-venv/Lib/site-packages/matplotlib/cbook.py:1699): ComplexWarning: Casting complex values to real discards the imaginary part
return math.isfinite(val)
[c:\Temp\seaborn_test\seaborn-venv\Lib\site-packages\pandas\core\dtypes\astype.py:134](file:///C:/Temp/seaborn_test/seaborn-venv/Lib/site-packages/pandas/core/dtypes/astype.py:134): ComplexWarning: Casting complex values to real discards the imaginary part
return arr.astype(dtype, copy=True)
[C:\Users\idunn\AppData\Local\Temp\ipykernel_40880\4206068624.py:3](file:///C:/Users/idunn/AppData/Local/Temp/ipykernel_40880/4206068624.py:3): RuntimeWarning: invalid value encountered in scalar divide
weightedSum = np.sum((weights * values)) / np.sum(weights)
[c:\Temp\seaborn_test\seaborn-venv\Lib\site-packages\numpy\lib\nanfunctions.py:1384](file:///C:/Temp/seaborn_test/seaborn-venv/Lib/site-packages/numpy/lib/nanfunctions.py:1384): RuntimeWarning: All-NaN slice encountered
return _nanquantile_unchecked(