当我们一次有多个数据,而且我们想要使用scanpy绘制umap图,或是spatial图的时候,大多数人一次只是绘制一个umap图或是一个spatial图在一个png或是一个pdf上面,对于我们可视化来说可能并没有那么轻便,于是需要做一些调整,借助于matplotlib的内参,让我们可以较为轻松的绘制多张图在一张图上面。代码比较容易理解,详细代码如下:

fig=plt.figure(figsize=(20,20))

for i in range(1,13):
    temp = adata[adata.obs['slices']==str(i)]
    # ax=fig.add_subplot(221)
    csv = pd.read_csv('/data/work/04.Transfer/RCTD/Result/all_batch/1014/' + str(i) + '.csv',sep = ' ')
    temp.obs['celltype'] = csv.first_type.tolist()
    axs = fig.add_subplot(4, 3, i)
    sc.pl.spatial(temp, 
                  color = 'celltype',
                  spot_size = 40, 
                  ax = axs, 
                  show = False, 
                  title = 'Slice_' + str(i) + '_celltype', 
                  frameon = False, 
                  legend_fontsize ='x-small',
                  legend_fontweight = 'normal',
                  
                  # legend_loc = None,  
                  # palette = spot_color
                 )

plt.savefig('all_batch_RCTD_TRANSFER.png')
plt.close()

fig=plt.figure(figsize=(20,12))



for i in range(1,41):
    temp = adata[adata.obs['batch']==str(i)]
    # ax=fig.add_subplot(221)
    axs = fig.add_subplot(4, 10, i)
    sc.pl.umap(temp, 
               color = 'leiden',
               # spot_size = 40, 
               size = 2,
               ax = axs, 
               show = False, 
               title = 'Slice' + str(i), 
               frameon = False, 
               legend_loc = None,  )

plt.savefig('figure/all_batch_harmony_leiden_umap.png')
plt.close()

这里面也有对于边框,对于字体的一些调整,其他的参数大家可以看matplotlib的详细内参。