上周,我们介绍了python的groupby函数和na值的填充,我们学会部分基础的数据处理方法。在获得基础数据后,我们为了探寻数据的规律,通常需要绘图挖掘数据意义。这里我们介绍python自带的基础绘图。
in [1]:
%matplotlib inline import matplotlib.pyplot as plt import pandas as pd
in [2]:
df = pd.read_excel('data/asv/phylum.xlsx') dfout[2]:
折线图
plot 默认参数 kind=line ,以下3种写法等同。
df.plot(x='phylum',y='st01')
df.plot(x='phylum',y='st01',kind='line')
df.plot.line(x='phylum',y='st01')
in [3]:
df.plot.line(x='phylum',y='st01') plt.show()out[3]:
x轴指定 phylum,由于phylum水平名称太长,故横坐标出来的图看不清。下图不指定x参数,则默认使用行名。
in [4]:
df.plot(y='st01') plt.show()out[4]:
in [5]:
df.plot(y=['st01','st02','st03','st04','st05']) plt.show()out[5]:
由图可知,5个样品趋势一致,且第4行和第24行的门是丰度最高的门。
条形图
in [6]:
df.plot.bar(x='phylum',y='st01') plt.show()out[6]:
in [7]:
df.plot.bar(x='phylum',y=['st01','st02','st03','st04','st05']) plt.show()out[7]:
kind = 'barh',则条形图水平绘制
in [8]:
df.plot.barh(x='phylum',y=['st01','st02','st03','st04','st05']) plt.show()out[8]:
散点图
in [9]:
df.plot.scatter(x='phylum',y='st01') plt.show()out[9]:
in [10]:
ax = df.plot.scatter(x='phylum',y='st01',color='green',label='st01') df.plot.scatter(x='phylum',y='st02',color='red',label='st02',ax=ax) plt.show()out[10]:
in [11]:
df.plot.scatter(x='st01',y='st02') plt.show()out[11]:
饼图
in [12]:
df.plot.pie(y='st01') plt.show()out[12]:
饼图参数是y=,从上图可以看出,给出的标签值有问题。
in [13]:
df.index = df.phylum df.plot.pie(y='st01',legend=false) plt.savefig('test.png')out[13]:
箱体图
in [14]:
df.plot.box(y='st01') plt.show()out[14]:
使用loc提取cyanobacteria、proteobacteria行的所有样品的丰度数据,并转置。此时cyanobacteria、proteobacteria变成列,对这两列数据绘制箱体图
df_sub.sample(5) 随机展示5行数据
in[15]:
df_sub = df.loc[['cyanobacteria','proteobacteria'],'st01':'st20'].t df_sub.sample(5)out[15]:
in [16]:
df_sub.plot.box(y=['cyanobacteria','proteobacteria']) plt.show()out[16]:
直方图
in[17]:
df_sub.plot.hist(y='cyanobacteria') plt.show()out[17]:
in [18]:
df_sub.plot.hist(y=['cyanobacteria','proteobacteria']) plt.show()out[18]:
同一类型的一页多图
in [19]:
df.plot(kind='line',y=['st01','st02','st03','st04'],subplots=true,layout=(2,2),figsize=(20,10),title="four sample's abundance") plt.show()
往期相关链接:
1、r基础篇
;
;
;
;
2、r进阶
;
;
;
;
;
;
;
;
;
3.python基础篇
;4、数据提交
;
;
;
;
5、表达谱分析
;;
;
6、医学数据分析
;;
;
;
天昊客户服务中心
手机/微信号:18964693703
【本群将为大家提供】
分享生信分析方案
提供数据素材及分析软件支持
定期开展生信分析线上讲座
qq号:1040471849
作者:大熊
审核:有才
来源:天昊生信团