# How to change the method of quartile computation that Seaborn uses to draw boxplots?

Suppose df is a variable assigned a pandas DataFrame object, whose collection of numeric columns includes one that is titled C.

The following question assumes that, when executing the call

seaborn.boxplot(data = df, y = 'C')

Seaborn invokes either numpy.quantile or numpy.percentile in order to determine where w.r.t. the y-axis to draw the lower and the upper sides of the box as well as the median line.

Now, numpy.quantile (and the same applies to numpy.percentile) has a parameter called method by which the method of computation of the quantile can be chosen from a list of at least 12 alternatives, as listed in the numpy.quantile documentation page. A different choice of method can yield a different output, in other words different numbers can be identified as the qth quartile of the same list of data points depending on which of these methods is used to compute the qth quartile.

How can I make Seaborn's boxplot function employ a different method of quartile computation than the default one?

I've come up with a convoluted, but effective, way to accomplish the desired effect.

The key is to define the following function establish_percentile_method. This function has two mandatory parameters, and it returns a value. The mandatory parameters are:

• method - a string, one of the possible legitimate arguments that can be passed to numpy.percentile's method parameter. For further details, see the numpy.percentile documentation page.
• og_percentile - a function object, which should act exactly like the percentile function as documented in the numpy documentation.

establish_percentile_method changes the numpy library's percentile variable to point to a function that acts almost exactly as documented for percentile in the numpy documentation, except that its method of computation is method.

The return value is a function that acts like percentile as described in the numpy documentation.

def establish_percentile_method(method, og_percentile):
def new_percentile(*args, **kwargs):
kwargs['method'] = method
return og_percentile(*args, **kwargs)
np.percentile = new_percentile
return og_percentile


Now you can use the function just defined to change the default method that numpy.percentile uses to calculate percentiles, and as a consequence affect every function that invokes numpy.percentile, such as pandas.describe() or seaborn.boxplot(...).

import numpy as np

def establish_percentile_method(method, og_percentile):
def new_percentile(*args, **kwargs):
if 'method' not in kwargs:
kwargs['method'] = method
return og_percentile(*args, **kwargs)
np.percentile = new_percentile
return og_percentile

import pandas as pd
import seaborn as sns

df = pd.DataFrame(data = [[1], [2], [3], [4]], columns = ['C'])
print(df) # Or, better, simply df, if you work in an interactive environment such as Google Colab.
df.describe()
sns.boxplot(data = df, y = 'C')

og_percentile = establish_percentile_method('inverted_cdf', np.percentile)
df.describe()
sns.boxplot(data = df, y = 'C')

establish_percentile_method('averaged_inverted_cdf', og_percentile)
df.describe()
sns.boxplot(data = df, y = 'C')

np.percentile = og_percentile
df.describe()
sns.boxplot(data = df, y = 'C')