怎么用Python绘图

这篇文章主要介绍“怎么用Python绘图”,在日常操作中,相信很多人在怎么用Python绘图问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Python绘图”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一. 数据准备

数据说明
示例数据,其中数据均为虚拟数据,与实际生物学过程无关
文件名: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 = 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轴标签

到此,关于“怎么用Python绘图”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!