【可视化】matplotlib—坝上繁星
2020-07-15 本文已影响0人
XuningFan
image.png
image.png
今天看文献的时候,看到一张图很经典,也很有意思,总结来说就是坝上繁星点点,不经想手动用python实现一下~~
巧妇难为无米之炊,来首先导入米~~
#! /usr/bin/env python3
import re,sys,os
import numpy as np
import pandas as pd
import matplotlib as mpl
mpl.use('Agg')
from matplotlib import pyplot as plt
plt.style.use('ggplot')
import seaborn as sns
from scipy import stats
其次设置main Figure:
ax=fig.add_axes([0.2,0.2,0.7,0.7],axisbg='w')
ax.tick_params(left='on',top='off',bottom='on',right='off')
for loc in ['left','bottom']:
ax.spines[loc].set_linewidth(1)
ax.spines[loc].set_color('k')
for loc in ['right','top']:
ax.spines[loc].set_visible(False)
根据每条染色体的signal画峰图:
sns.swarmplot(x="Samples",y="signal",color='k',data=tad_sig_df,ax=ax)
#tad_sig_df (三列分别为 sample chr signal)
#1_week chr1 0.2124825
#1_week chr2 0.2491146
# .........
#2_week chr1 0.2138446
colors=['#84632E','#D27A79','#C84442','#822329','#2F4898','#2C4D60']
根据每条染色体的signal 画bar图:
plt.bar([i-0.4 for i in list(range(0,8))],sam_sig_df['signal'],color=colors)
#sam_sig_df (两列:sample signal)
#1_week 0.2327716
#2_weeks 0.2401719
#3_weeks 0.2437665
#4_weeks 0.2499037
#5_weeks 0.3866738
#6_weeks 0.401157
#7_weeks 0.4333483
#8_weeks 0.3651956
ax.set_ylabel("Relative variance of TAD signal")
ax.set_xticks([i+0.1 for i in list(range(sam_sig_df.shape[0]))])
ax.set_xticklabels(sam_sig_df['sample'],rotation=45)
ax.set_ylim(0,0.7)
根据秩和检验计算两两样本之间的p值。
首先整理数据格式如下:
#df2(nxn的矩阵)
# chrom 1_week 2_week 3_week 4_week 5_week 6_week 7_week 8_week
# chr1 0.2124825 0.2138446 0.2681415 0.2320308 0.3648542 0.3719555 0.4154264 0.3394036
# chr2 0.2491146 0.2554405 0.2567992 0.2650865 0.4097313 0.4253969 0.4587423 0.3856153
# chr3 0.2205689 0.22094 0.2214508 0.2258953 0.3572473 0.3769948 0.4211215 0.3438275
# chr4 0.2208674 0.247468 0.2432454 0.2639721 0.4302134 0.4141755 0.4414896 0.3693487
# chr5 0.2237626 0.2580336 0.2526648 0.255073 0.3984676 0.4200537 0.4381874 0.3612952
# chr6 0.2415278 0.2400543 0.2517413 0.239386 0.3853607 0.4017974 0.4341992 0.3602986
# chr7 0.2336476 0.234182 0.2414343 0.2406793 0.3555941 0.3677946 0.3882644 0.3354686
# chr8 0.2282274 0.2357507 0.2349158 0.2397335 0.4051282 0.414629 0.4580185 0.3892537
# chr9 0.2342663 0.2385664 0.23559 0.2473451 0.3881347 0.399435 0.4376989 0.3636179
# chr10 0.2438228 0.242409 0.243789 0.2464791 0.404039 0.4146366 0.4514503 0.3739338
# chr11 0.2355708 0.2506878 0.2361022 0.259202 0.3887114 0.4084879 0.4376043 0.3648314
# chr12 0.2668528 0.2573399 0.2602321 0.2681254 0.4455759 0.4686056 0.486874 0.4527546
# chr13 0.2642351 0.2991617 0.288949 0.3143113 0.4398373 0.4596589 0.526111 0.4121843
# chr14 0.223002 0.2101223 0.2196958 0.2623507 0.3686394 0.4220504 0.4296982 0.3881499
# chr15 0.2181983 0.2297351 0.2189778 0.2365727 0.3629807 0.3722136 0.4108971 0.3422202
# chr16 0.2369795 0.2387444 0.2402405 0.2416215 0.3731693 0.3903155 0.4269084 0.3636249
# chr17 0.225208 0.2342196 0.2283373 0.2354873 0.3276592 0.3425167 0.3788839 0.3140054
# chr18 0.235575 0.254464 0.2422985 0.2628256 0.4198472 0.4279283 0.4684907 0.3979184
# chr19 0.2512119 0.2601831 0.2505591 0.2589785 0.4025605 0.420783 0.4507594 0.3899232
# chrX 0.2071519 0.1946505 0.2140269 0.2163633 0.3106201 0.3144296 0.317807 0.3004519
循环计算p值
for i in list(range(1,len(sams))):
sam1=np.array(df2.iloc[:,i])
sam2=np.array(df2.iloc[:,i+1])
all_sams=np.ravel([sam1,sam2])
height=np.max(all_sams)
stat,pval=stats.ranksums(sam1,sam2)
ax.annotate("", xy=(i-0.9,height+0.03), xycoords='data',xytext=(i+0.1,height+0.03),textcoords='data',arrowprops=dict(arrowstyle="-",ec ='#aaaaaa',connectionstyle="arc3,rad=0"))
sig='%.3f'%pval
if pval<0.05:
sig='*'
if pval<0.001:
sig='**'
if pval <0.005:
sig='***'
ax.text((i-0.4),height+0.05,sig,horizontalalignment='center',verticalalignment='center')
大功告成,保存图片~~
fig.savefig("./example.pdf")
最后成图如下:
image.png