-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkde_density.py
27 lines (22 loc) · 872 Bytes
/
kde_density.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def kde_density(adata, x, y, nbins=300, cmap=None, figsize=[4,4]):
"""
adata: The annotated data matrix.
x: a gene's expression at x coordinate
y: a gene's expression at y coordinate
nbins: number of bins for the kernel density estimation
# usage
kde_density(adata, x='Gata3',y='Id2',cmap=plt.cm.Greys)
"""
# libraries
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import kde
x= np.array(adata[:,x].X.todense()).reshape(-1)
y= np.array(adata[:,y].X.todense()).reshape(-1)
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
plt.figure(figsize=figsize)
# Make the plot
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), shading='auto', cmap=cmap)
plt.show()