<jupyter_start><jupyter_text>Custom KDE plot in matplotlib<jupyter_code>import numpy as np

import matplotlib.pyplot as plt
from matplotlib import colors
%matplotlib inline

%load_ext autoreload
%autoreload 2
rs = np.random.RandomState(8)
Nsample = 30
N = 500
x = np.linspace(0, 1.0, Nsample)
y = np.empty((N, Nsample))
for i in range(N):
    y[i] = np.sin(x*(2*np.pi)) + 0.25 * rs.normal(0, 1.0, 30) + rs.binomial(2, 0.3)
lines = plt.plot(x, y.T, color='b', alpha=0.2)
from scipy import stats

def kdeplot(x, y, color='b', alphamax=1.0, ax=None):
    if ax is None:
        ax = plt.gca()
    kde_data = []
    y_min, y_max = y.min(), y.max()
    y_vals = np.linspace(y_min, y_max, len(y))
    for pt_data in y.T:
        pt_kde = stats.kde.gaussian_kde(pt_data)
        kde_data.append(pt_kde(y_vals))
    kde_data = np.transpose(kde_data)
    rgb = colors.ColorConverter().to_rgb(color)
    img = np.zeros((kde_data.shape[0], kde_data.shape[1], 4))
    img[:, :, :3] = rgb
    kde_data /= kde_data.max(axis=0)
    img[:, :, 3] = kde_data * alphamax
    ax.imshow(img, interpolation="bicubic",
              extent=(x.min(), x.max(), y_min, y_max),
              aspect="auto", origin="lower")
kdeplot(x, y, alphamax = 0.75)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16560509554140126, 'fraction_numerical': 0.02786624203821656, 'mean_word_length': 3.441696113074205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '453831', 'license': 'MIT', 'n_tokens_mistral': 561, 'n_tokens_neox': 540, 'n_words': 113, 'path': 'plot-kde-trace.ipynb', 'repo_name': 'andim/pysnippets', 'size': '1256'}