今天我們的任務(wù)是實(shí)現(xiàn)下面這張圖
多樣本整合分析降維聚類之后填物,每個樣本分布的二維空間密度圖祈争,可以顯示每個樣本在空間上的分布差異店枣,體現(xiàn)的是樣本之間的差異旺上。
我不知道大家喜歡R畫圖還是python畫圖辟狈,個人喜歡python風(fēng)格肠缔,今天我們就以python來實(shí)現(xiàn)上面這張圖。
先來一個簡單的,隨機(jī)產(chǎn)生的數(shù)據(jù)繪制的散點(diǎn)密度圖
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
x = np.random.normal(size=1000)
y = x * 3 + np.random.normal(size=1000)
# Calculate the point density
xy = np.vstack([x, y])
z = gaussian_kde(xy)(xy)
fig, ax = plt.subplots()
plt.scatter(x, y, c=z, s=10, edgecolor='',cmap = 'Reds')
plt.tick_params(labelsize=15)
plt.xticks(size = 15,family = 'Times New Roman')
plt.yticks(size = 15,family = 'Times New Roman')
plt.xlabel('x',size = 20,family = 'Times New Roman')
plt.ylabel('y',size = 20,family = 'Times New Roman')
cb = plt.colorbar(shrink = 0.5)
cb.ax.tick_params(labelsize=15)
for l in cb.ax.yaxis.get_ticklabels():
l.set_family('Times New Roman')
plt.figtext(0.76,0.73,'density',size = 20,family = 'Times New Roman')
plt.show()
上面這樣圖我們打個樣哼转,接下來就是我們單細(xì)胞的數(shù)據(jù)了
準(zhǔn)備數(shù)據(jù)(多樣本單細(xì)胞聚類數(shù)據(jù))
二維降維坐標(biāo)和樣本信息明未,開始畫圖
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
os.chdir('C:/Users/86178/Desktop')
data = pd.read_csv('XZNYLvsXZNYL_3vsXZNYL_4vsXZNYL_7_UMAP_sample.csv',index_col = 0)
x = data.loc[:,'UMAP_1']
y = data.loc[:,'UMAP_2']
# Calculate the point density every sample
data1 = data.loc[data.Sample == 'Sample1',:] ##第一個樣本
x1 = data1.loc[:,'UMAP_1']
y1 = data1.loc[:,'UMAP_2']
xy = np.vstack([x1, y1])
z = gaussian_kde(xy)(xy)
fig, ax = plt.subplots()
plt.scatter(x, y, s=10, edgecolor='',c = 'grey')
plt.scatter(x1,y1,s = 2,c = z,edgecolor='',cmap = 'Reds')
plt.tick_params(labelsize=15)
plt.xticks(size = 15,family = 'Times New Roman')
plt.yticks(size = 15,family = 'Times New Roman')
plt.xlabel('UMAP_1',size = 20,family = 'Times New Roman')
plt.ylabel('UMAP_2',size = 20,family = 'Times New Roman')
cb = plt.colorbar(shrink = 0.5)
cb.ax.tick_params(labelsize=15)
for l in cb.ax.yaxis.get_ticklabels():
l.set_family('Times New Roman')
plt.figtext(0.76,0.73,'density',size = 20,family = 'Times New Roman')
plt.title('Sample1',size = 25,family = 'Times New Roman')
plt.show()
但是大家注意看,我們想要完成的圖片colorbar是0到1壹蔓,說明這里的密度值趟妥,我們需要處理一下,進(jìn)行縮放佣蓉,大家注意下面的代碼和上面代碼的不同之處
import numpy as np
import pandas as pd
import os
from sklearn import preprocessing
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
os.chdir('C:/Users/86178/Desktop')
data = pd.read_csv('XZNYLvsXZNYL_3vsXZNYL_4vsXZNYL_7_UMAP_sample.csv',index_col = 0)
x = data.loc[:,'UMAP_1']
y = data.loc[:,'UMAP_2']
# Calculate the point density every sample
data1 = data.loc[data.Sample == 'Sample1',:]
x1 = data1.loc[:,'UMAP_1']
y1 = data1.loc[:,'UMAP_2']
xy = np.vstack([x1, y1])
z = gaussian_kde(xy)(xy)
z = preprocessing.maxabs_scale(z,axis=0, copy=True)
fig, ax = plt.subplots()
plt.scatter(x, y, s=2, edgecolor='',c = 'grey')
plt.scatter(x1,y1,s = 5,c = z,edgecolor='',cmap = 'Reds')
plt.tick_params(labelsize=15)
plt.xticks(size = 15,family = 'Times New Roman')
plt.yticks(size = 15,family = 'Times New Roman')
plt.xlabel('UMAP_1',size = 20,family = 'Times New Roman')
plt.ylabel('UMAP_2',size = 20,family = 'Times New Roman')
cb = plt.colorbar(shrink = 0.5)
cb.ax.tick_params(labelsize=15)
for l in cb.ax.yaxis.get_ticklabels():
l.set_family('Times New Roman')
plt.figtext(0.76,0.73,'density',size = 20,family = 'Times New Roman')
plt.title('Sample1',size = 25,family = 'Times New Roman')
plt.show()
然后子集隨意選擇數(shù)據(jù)集
import numpy as np
import pandas as pd
import os
from sklearn import preprocessing
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
os.chdir('C:/Users/86178/Desktop')
data = pd.read_csv('XZNYLvsXZNYL_3vsXZNYL_4vsXZNYL_7_UMAP_sample.csv',index_col = 0)
x = data.loc[:,'UMAP_1']
y = data.loc[:,'UMAP_2']
# Calculate the point density every sample
data1 = data.loc[data.Sample == 'Sample2',:]
x1 = data1.loc[:,'UMAP_1']
y1 = data1.loc[:,'UMAP_2']
xy = np.vstack([x1, y1])
z = gaussian_kde(xy)(xy)
z = preprocessing.maxabs_scale(z,axis=0, copy=True)
fig, ax = plt.subplots()
plt.scatter(x, y, s=2, edgecolor='',c = 'grey')
plt.scatter(x1,y1,s = 5,c = z,edgecolor='',cmap = 'Reds')
plt.tick_params(labelsize=15)
plt.xticks(size = 15,family = 'Times New Roman')
plt.yticks(size = 15,family = 'Times New Roman')
plt.xlabel('UMAP_1',size = 20,family = 'Times New Roman')
plt.ylabel('UMAP_2',size = 20,family = 'Times New Roman')
cb = plt.colorbar(shrink = 0.5)
cb.ax.tick_params(labelsize=15)
for l in cb.ax.yaxis.get_ticklabels():
l.set_family('Times New Roman')
plt.figtext(0.76,0.73,'density',size = 20,family = 'Times New Roman')
plt.title('Sample2',size = 25,family = 'Times New Roman')
plt.show()
大家學(xué)會了吧披摄,多多學(xué)習(xí)
生活很好,有你更好