Seaborn выбирает ужасный xlim при построении KDE для больших значений - PullRequest
0 голосов
/ 13 июля 2020

Когда seaborn.kdeplot применяется к данным, состоящим из больших значений, seaborn кажется неспособным распознать, что сгенерированные хвосты функции распределения вероятностей незначительны и не должны быть включены в график.

MWE:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
np.random.seed(0)

x = np.random.randn(100)
sx = x + 10**14 # shifted x
sxlim = (np.min(sx),np.max(sx))

#plot = sns.kdeplot
plot = sns.distplot

plt.figure()
plt.subplot(311)
plot(x)
plt.subplot(312)
plot(sx)
plt.subplot(313)
plot(sx)
plt.xlim(sxlim)
plt.tight_layout()

graph output of MWE

We can see that user has a reason to expect shape of displayed graph to be invariant to translation and scaling from the following examples. However, seaborn only does this for upto artbitrary size of input values.

def plot_matrix(transform,title):
    plt.figure()
    ts = [0,3,6,9,12,13,14,15,16]
    for i,t in enumerate(ts,1):
        plt.subplot(3,3,i)
        plot(transform(x,t))
        plt.title(title(t))
    plt.tight_layout()

plot_matrix(
    (lambda x,t: x + 10**t),
    (lambda t:  'x + 10**{}'.format(t))
)
plot_matrix(
    (lambda x,t: x * 10**t),
    (lambda t:  'x * 10**{}'.format(t))
)
plt.show()

distplots with increasingly bigger translation

distplots with increasingly bigger translation

distplots with increasingly bigger scaling

дистрибутивы с увеличивающимся масштабированием

Вопрос

Как лучше всего всегда иметь правильные xlim для seaborn.kdeplot и seaborn.distplot?

  • Может быть, есть опция easy 'treshold' где-то в matplotlib для управления xlim автоматическим c определением?
  • Может быть, seaborn предоставляет такую ​​возможность?
  • Может быть, это еще не предусмотрено. Что бы вы выбрали тогда?
    • взять (min - .02 * range, max + .02 * range) как xlim?
    • показать график, покрывающий 95% области под KDE?
...