Matplotlib初体验

为一个客户做了关于每个差异otu在时间点上变化的折线图,使用python第一次做批量作图的程序,虽然是很简单的折线图,但是也是第一次使用matplotlib的纪念。

ps:在第一个脚本上做了点小的改动,加上了分类信息作为图的标题,加上网格便于对照y轴丰度值,x轴的名称更加接近样品的实际名称。

 1 from __future__ import division
 2 import numpy as np
 3 import matplotlib.pyplot as plt
 4 from matplotlib.ticker import MultipleLocator, FormatStrFormatter
 5 import sys
 6 
 7 def main():
 8     file = sys.argv[1]
 9     x = np.array([0,6,12,18])
10     mean_file = ['../c-%dm_vs_cf-%dm.mean.profile'%(t,t) for t in [0,6,12,18]]
11     with open(file) as f:
12         f.next()
13         for line in f:
14             C = []
15             CF = []
16             tabs = line.strip().split('\t')
17             otu_name = tabs.pop(0)
18             for file in mean_file:
19                 (C_temp,CF_temp) = getMeanProfile(otu_name,file)
20                 C.append(float(C_temp))
21                 CF.append(float(CF_temp))
22             C = np.array(C)
23             CF = np.array(CF)
24             tax_name = tax[otu_name]
25             do_plot(tax_name,otu_name,x,C,CF)
26         f.close()
27 
28 def getMeanProfile(otu_name,file):
29     handle = open(file)
30     handle.next()
31     for line in handle:
32         tabs = line.strip().split('\t')
33         if otu_name != tabs[0]:
34             continue
35         return( (tabs[1],tabs[2]) )
36     handle.close()
37 
38 def do_plot(title,file_name,x,y1,y2):
39     plt.figure(figsize=(10,6))
40     ax = plt.subplot(111)
41     plt.plot(x,y1,label="C",color="red",linewidth=2)
42     plt.plot(x,y2,label="CF",color="blue",linewidth=2)
43     xmajorLocator = MultipleLocator(6)
44     xmajorFormatter = FormatStrFormatter('%dm')
45     yRange = ( max(np.max(y1),np.max(y2)) - min(np.min(y1),np.min(y2)) )
46     ymajorLocator = MultipleLocator(yRange/10)
47     yminorLocator = MultipleLocator(yRange/40)
48     ax.xaxis.set_major_locator(xmajorLocator)
49     ax.xaxis.set_major_formatter(xmajorFormatter)
50     ax.yaxis.set_major_locator(ymajorLocator)
51     ax.yaxis.set_minor_locator(yminorLocator)
52     ax.xaxis.grid(True,which='major')
53     ax.yaxis.grid(True,which='minor')
54     plt.xlabel("Month(s)")
55     plt.ylabel("mean_profile")
56     plt.title(title)
57     plt.legend()
58     plt.savefig("%s.png"%file_name,dpi=80)
59 
60 def getTax():
61     for line in open('../tax.txt'):
62         tabs = line.strip().split('\t')
63     for line in open('../tax.txt'):
64         tabs = line.strip().split('\t')
65         tax[tabs[0]] = tabs[1].split(';')[-1]
66 
67 if __name__ == '__main__':
68     tax = {}
69     getTax()
70     main()

 

转载于:https://www.cnblogs.com/lyon2014/p/4528493.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值