密度散点图的绘制(画图基本知识)
2021-06-22 本文已影响0人
单细胞空间交响乐
今天我们的任务是实现下面这张图
图片.png多样本整合分析降维聚类之后,每个样本分布的二维空间密度图,可以显示每个样本在空间上的分布差异,体现的是样本之间的差异。
我不知道大家喜欢R画图还是python画图,个人喜欢python风格,今天我们就以python来实现上面这张图。
先来一个简单的,随机产生的数据绘制的散点密度图
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()
图片.png
上面这样图我们打个样,接下来就是我们单细胞的数据了
准备数据(多样本单细胞聚类数据)
图片.png二维降维坐标和样本信息,开始画图
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()
图片.png
但是大家注意看,我们想要完成的图片colorbar是0到1,说明这里的密度值,我们需要处理一下,进行缩放,大家注意下面的代码和上面代码的不同之处
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()
图片.png
然后子集随意选择数据集
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()
图片.png
大家学会了吧,多多学习
生活很好,有你更好