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()
python 圈图
最新推荐文章于 2022-04-18 01:08:00 发布