python 圈图

import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from itertools import chain
from pandas import Series,DataFrame
from pylab import mpl

%matplotlib inline
plt.rc('font',family='Times New Roman') 
mpl.rcParams['font.size']=12

os.chdir('e://test/')
df=pd.read_excel('e://test/OTU.xlsx',sheet_name='调整')
def Unknown_abundance_ratio(x,z):
    result=[]
    result1=[]
    df2=pd.pivot_table(df,index=z,aggfunc='sum')
    df3=df2.drop('SUM',axis=1)
    df4=df3[df3.index.str.contains(x)]
    len(df4)
    sum1=(z,x,len(df4.index),len(df3.index))
    result.append(sum1)    
    return result
unknown=['unclassified','uncultured','unidentified']
h=[]
for z in df.columns[4:6]:   
    for i in range(len(unknown)):
        x=unknown[i]
        h.append(Unknown_abundance_ratio(x,z))        
frequency=chain.from_iterable(h)
frequency=pd.DataFrame(frequency,columns=['级别','分类情况','次数','总次数'])#.to_excel('d://x//20200801/OTU(次数).xlsx')


frequency
I=frequency['次数']
# print(I)
Q=I[:3].sum()
# print(frequency['总次数'][0]-Q)
A=I[3:].sum()
# print(frequency['总次数'][5]-A)
#查看数据得出(OTU(次数)))
x=[508,55,0,0]
y=[195,315,410,6]
m=[1]
#看数据得出 (OTU(result))
ratio1=[1-0.054184,0.054184,0.000000,0.000000]
ratio2=[1-0.521506-0.313251-0.000920,0.521506,0.313251,0.000920]
S=['indentified','unclassified','unidentified','uncultured']

colors = ['#0000FF', "crimson", "green", "#ff7f00"]
a=["#F0F0F0"]


fig = plt.figure(figsize=(10,10),dpi=600)

plt.subplot(2,2,1)
plt.pie(x,labels=x,radius=1.2,colors=colors,wedgeprops={'linewidth': 1, 'edgecolor': "black"})
plt.pie(ratio1,radius=1,colors=colors,wedgeprops={'linewidth': 1, 'edgecolor': "black"},autopct="%.2f%%")
plt.pie(m,radius=0.8,colors=a,wedgeprops={'linewidth': 1, 'edgecolor': "black"})
plt.title('Genus')
plt.subplot(2,2,2)
plt.pie(y,labels=y,radius=1.2,colors=colors,wedgeprops={'linewidth': 1, 'edgecolor': "black"})
plt.pie(ratio2,radius=1,colors=colors,wedgeprops={'linewidth': 1, 'edgecolor': "black"},autopct="%3.2f%%")
plt.pie(m,radius=0.8,colors=a,wedgeprops={'linewidth': 1, 'edgecolor': "black"})
plt.title('Species')

plt.legend(labels=S,loc='upper center',bbox_to_anchor=(-0.25,1),ncol=1)
plt.tight_layout()
fig.set_tight_layout(True)
fig.savefig('Species(dpi=300).png',dpi=300,bbox_inches='tight')
plt.show()

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值