論文
De novo assembly, annotation, and comparative analysis of 26 diverse maize genomes
部分?jǐn)?shù)據(jù)和代碼是公開的因宇,我們今天試著重復(fù)一下論文補(bǔ)充材料里的 Figure S29
這個(gè)熱圖是用python中的seaborn模塊畫的,下面介紹畫圖代碼
導(dǎo)入需要用到的模塊
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
讀入數(shù)據(jù)集
部分?jǐn)?shù)據(jù)截圖如下
file = "matrix-b73-ref.csv"
b73Ref = pd.read_csv(file, index_col=0).reindex(["B97", "Ky21", "M162W",
"Ms71", "Oh43", "Oh7B", "M37W", "Mo18W", "Tx303", "HP301", "P39",
"Il14H", "CML52", "CML69", "CML103", "CML228", "CML247", "CML277",
"CML322", "CML333", "Ki3", "Ki11", "NC350", "NC358", "Tzi8"])
b73Ref = b73Ref[["B97", "Ky21", "M162W",
"Ms71", "Oh43", "Oh7B", "M37W", "Mo18W", "Tx303", "HP301", "P39",
"Il14H", "CML52", "CML69", "CML103", "CML228", "CML247", "CML277",
"CML322", "CML333", "Ki3", "Ki11", "NC350", "NC358", "Tzi8"]]
這里 index_col=0
是用數(shù)據(jù)集中的第一列來(lái)做行名
reindx()
函數(shù)是將行按照自己制定的內(nèi)容排序
[[]]
是把列按照指定的內(nèi)容排序
查看數(shù)據(jù)集的前5行
b73Ref.head(5)
最基本的熱圖
sns.heatmap(b73Ref)
只保留下三角
這里直接讀取的數(shù)據(jù)集的數(shù)據(jù)類型是整數(shù)型娇钱,我們需要把數(shù)據(jù)轉(zhuǎn)換為浮點(diǎn)型效斑。論文中提供的代碼是沒(méi)有轉(zhuǎn)換數(shù)據(jù)類型的弃榨,如果完全按照他的代碼運(yùn)行可能會(huì)遇到報(bào)錯(cuò),這里可能是因?yàn)閜ython的版本不同吧锭弊,我現(xiàn)在用的python是3.8.3
colnames = ["B97", "Ky21", "M162W",
"Ms71", "Oh43", "Oh7B", "M37W", "Mo18W", "Tx303", "HP301", "P39",
"Il14H", "CML52", "CML69", "CML103", "CML228", "CML247", "CML277",
"CML322", "CML333", "Ki3", "Ki11", "NC350", "NC358", "Tzi8"]
dtype = {}
for colname in colnames:
dtype[colname] = np.float64
df = b73Ref.astype(dtype)
mask = np.triu(np.ones_like(df,dtype=bool))
sns.heatmap(df,mask=mask)
這里 np.ones_like()
函數(shù)是生成一個(gè)和指定數(shù)據(jù)集維度一樣的矩陣 數(shù)值全是1
np.triu()
函數(shù)是將下三角變成0或者false
更改配色
cmap = sns.diverging_palette(370, 120, n=80, as_cmap=True)
sns.heatmap(df, mask=mask, cmap=cmap, robust=True,
square=True, linewidths=.5, cbar_kws={"shrink": .5})
添加輔助線,去掉y軸標(biāo)題
f, ax = plt.subplots(figsize=(14, 14))
cmap = sns.diverging_palette(370, 120, n=80, as_cmap=True)
sns.heatmap(df, mask=mask, cmap=cmap, robust=True,
square=True, linewidths=.5, cbar_kws={"shrink": .5})
plt.ylabel('')
ax.axvline(x=6, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.76)
ax.axvline(x=9, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.64)
ax.axvline(x=10, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.6)
ax.axvline(x=12, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.52)
ax.axhline(y=6, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.24)
ax.axhline(y=9, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.36)
ax.axhline(y=10, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.4)
ax.axhline(y=12, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.48)
給坐標(biāo)軸的標(biāo)簽賦予顏色
f, ax = plt.subplots(figsize=(14, 14))
cmap = sns.diverging_palette(370, 120, n=80, as_cmap=True)
sns.heatmap(df, mask=mask, cmap=cmap, robust=True,
square=True, linewidths=.5, cbar_kws={"shrink": .5})
plt.ylabel('')
ax.axvline(x=6, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.76)
ax.axvline(x=9, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.64)
ax.axvline(x=10, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.6)
ax.axvline(x=12, color ='blue', lw = 1.5, alpha = 0.75, ymax = 0.52)
ax.axhline(y=6, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.24)
ax.axhline(y=9, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.36)
ax.axhline(y=10, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.4)
ax.axhline(y=12, color ='black', lw = 1.5, alpha = 0.75, xmax = 0.48)
mycol = ["#4169E1", "#4169E1", "#4169E1", "#4169E1", "#4169E1", "#4169E1", "#787878", "#787878", "#787878", "#DA70D6", "#FF4500", "#FF4500", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32", "#32CD32"]
for tick, color in zip(ax.get_xticklabels(), mycol): tick.set_color(color)
for tick, color in zip(ax.get_yticklabels(), mycol): tick.set_color(color)
plt.savefig("1.pdf")
這個(gè)是最終的結(jié)果
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1债沮、R語(yǔ)言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡(jiǎn)單小例子;2蝇裤、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)廷支、基因組學(xué)频鉴、群體遺傳學(xué)文獻(xiàn)閱讀筆記栓辜;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記垛孔!