前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,如有問題請及時聯系我們以作處理,
作者:小湯豆
來源:湯豆道課
Python爬蟲、資料分析、網站開發等案例教程視頻免費在線觀看
https://space.bilibili.com/523606542
一. 資料準備
資料說明
示例資料,其中資料均為虛擬資料,與實際生物學程序無關
檔案名:dataset_volcano.txt
列分別為基因 (gene),差異倍數(logFC),t-test的P值(P.Value)
二. 繪制火山圖
先上效果圖:
Step 1: 匯入資料:
import pandas as pd # Data analysis import numpy as np # Scientific computing import seaborn as sns # Statistical visualization # 讀取資料 df = pd.read_csv('./dataset_volcano.txt', sep='\t') result = pd.DataFrame() result['x'] = df['logFC'] result['y'] = df['P.Value'] result['-log10(pvalue)']=-df['P.Value'].apply(np.log10)
Step2: 設定閾值
# 設定pvalue和logFC的閾值 cut_off_pvalue = https://www.cnblogs.com/hhh188764/p/0.0000001 cut_off_logFC = 1
Step3: 設定分組
#分組為up, normal, down result.loc[(result.x> cut_off_logFC )&(result.y < cut_off_pvalue),'group'] = 'up' result.loc[(result.x< -cut_off_logFC )&(result.y < cut_off_pvalue),'group'] = 'down' result.loc[(result.x>=-cut_off_logFC )&(result.x<=cut_off_logFC )|(result.y >= cut_off_pvalue),'group'] = 'normal'
Step4: 繪制散點圖
#繪制散點圖 ax = sns.scatterplot(x="x", y="-log10(pvalue)", hue='group', hue_order = ('down','normal','up'), palette=("#377EB8","grey","#E41A1C"), alpha=0.5, s=15, data=result)
Step5: 設定散點圖
#確定坐標軸顯示范圍 xmin=-6 xmax=10 ymin=7 ymax=13 ax.spines['right'].set_visible(False) #去掉右邊框 ax.spines['top'].set_visible(False) #去掉上邊框 ax.vlines(-cut_off_logFC, ymin, ymax, color='dimgrey',linestyle='dashed', linewidth=1) #畫豎直線 ax.vlines(cut_off_logFC, ymin, ymax, color='dimgrey',linestyle='dashed', linewidth=1) #畫豎直線 ax.hlines(-np.log10(cut_off_pvalue), xmin, xmax, color='dimgrey',linestyle='dashed', linewidth=1) #畫豎水平線 ax.set_xticks(range(xmin, xmax, 4))# 設定x軸刻度 ax.set_yticks(range(ymin, ymax, 2))# 設定y軸刻度 ax.set_ylabel('-log10(pvalue)',fontweight='bold') # 設定y軸標簽 ax.set_xlabel('log2(fold change)',fontweight='bold') # 設定x軸標簽
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250462.html
標籤:Python

