函数实现
import scanpy as sc adata = sc.datasets.pbmc68k_reduced()
print(adata)
sc.pl.umap(adata,color=["bulk_labels"])def change_show_color(adata,label,category_list=None,color_list=None):for i in range(len(color_list)):if(len(color_list[i])==7):color_list[i]=color_list[i]+"ff" if(label is None):print("Please give the label name which will be used to display")#else if(category_list is None or color_list is None):elif(color_list is None):print("no color need to be changed")return adataelif(label not in adata.obs.columns):print("Please provide the corrected category name which must be found in adata.obs")elif(len(category_list)!=len(color_list)):print("the length of category list is not equal to color list!")else:for i in range(len(category_list)):change_cat = category_list[i]change_color = color_list[i]ind = list(adata.obs[label].cat.categories).index(change_cat)adata.uns[f"{label}_colors"][ind] = change_color #return adatacategory_list = ["CD34+", "Dendritic","CD56+ NK"]
color_list = ['#ff0000ff',"red","#ff0000"]adata = change_show_color(adata,"bulk_labels",category_list,color_list)
sc.pl.umap(adata,color=["bulk_labels"])
结果如下
其他的方式
import scanpy as sc adata = sc.datasets.pbmc68k_reduced()
print(adata)sc.pl.umap(adata,color=["bulk_labels"],palette={"Dendritic":"red",
"CD14+ Monocyte":"red" ,
"CD19+ B" :"red" ,
"CD4+/CD25 T Reg" :"red" ,
"CD8+ Cytotoxic T" :"red" ,
"CD8+/CD45RA+ Naive Cytotoxic" :"red",
"CD56+ NK" :"red" ,
"CD4+/CD45RO+ Memory" :"red",
"CD34+" :"red" ,
"CD4+/CD45RA+/CD25- Naive T" :"red",})
adata.uns["bulk_labels_colors"]