数据可视化,是希望通过一个直观的图例来展示自己的结论。
本实验是将一个目录下的多个数据集 中的代码修改量和软件缺陷的关系 通过数据可视化展示。
总体思路:就是先把73个项目的修改数存入churn中,然后从小到大排序,把churn里面的数据分成10份,如第一份是修改量的前百分之十,然后统计这百分之十有多少bug数
最后图是每份里面的bug数量占比
一、加载模块
import pandas as pd
import os
import matplotlib.pyplot as plt
from pylab import mpl
import csv
二、前期准备
path=r"C:\\Users\\86189\\Desktop\\学习资料\\大一\\python\\dataModified\\"
files=os.listdir(path)
churn=[]
dic={}
for j in range(0,10):
dic[str(j*10)+"%~"+str(j*10+10)+"%"]=0
#将所有文件名存入files中
将文件夹的路径赋值给path,用os.path获取这个目录下的所有文件名 放在files中。
初始化列表churn为空列表,字典dic所有键对应的值都为0。
三、列表 字典
for i in files:
path=r"C:\\Users\\86189\\Desktop\\学习资料\\大一\\python\\dataModified\\"+i
with open(path,'r',encoding='utf-8')as stucsv:
reader=csv.reader(stucsv)
for row in reader:
la=float(row[6])
ld=float(row[7])
if row[16]=='0':
la=int(la)
ld=int(ld)
churn.append(la+ld)
churn=sorted(churn)
#将有软件缺陷的代码修改量存入churn,并排序.
#其中含有软件缺陷的是float型数字,用来标记
length=len(churn)
a=0
b=10
c=0
d=int(length/10)
e=int(length/10)
for item in churn:
if d<length:
if item>churn[d] and a<90:
a=a+10
c=d
d=d+e
count=0
if d<length:
if item>=churn[c] and item<=churn[d] :
if '.'in str(item):
dic[str(a)+"%~"+str(a+10)+"%"]=dic[str(a)+"%~"+str(a+10)+"%"]+1
print(i+' 正在运行')
#将每个项目的修改量的0~10%,10~20%……,每一段里面 总的软件缺陷数量 存入字典
写了一个循环,每一个文件对应一次循环。
将代码修改量(la+ld)存入churn列表中,然后从小到大进行排序,将churn里的所有项目分为十份 为:0~10%,10~20%……,将每一份里的代码修改量存入字典。
四、画图
labels=list(dic.keys())
sizes=list(dic.values())
mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.title('代码修改量和软件缺陷的关系')
plt.pie(sizes,labels=labels,autopct='%1.1f%%',shadow=False, textprops={'fontsize': 8, 'color': 'w'},startangle=90)
plt.axis('equal')
plt.legend(loc='lower right')
plt.show()
#将上面字典的键名作为labels,画图
五、完整代码
import pandas as pd
import os
import matplotlib.pyplot as plt
from pylab import mpl
import csv
path=r"C:\Users\86189\Desktop\学习资料\大一\python\\dataModified\\"
files=os.listdir(path)
churn=[]
dic={}
for j in range(0,10):
dic[str(j*10)+"%~"+str(j*10+10)+"%"]=0
#将所有文件名存入files中
for i in files:
path=r"C:\Users\86189\Desktop\学习资料\大一\python\\dataModified\\"+i
with open(path,'r',encoding='utf-8')as stucsv:
reader=csv.reader(stucsv)
for row in reader:
la=float(row[6])
ld=float(row[7])
if row[16]=='0':
la=int(la)
ld=int(ld)
churn.append(la+ld)
churn=sorted(churn)
#将有软件缺陷的代码修改量存入churn,并排序.
#其中含有软件缺陷的是float型数字,用来标记
length=len(churn)
a=0
b=10
c=0
d=int(length/10)
e=int(length/10)
for item in churn:
if d<length:
if item>churn[d] and a<90:
a=a+10
c=d
d=d+e
count=0
if d<length:
if item>=churn[c] and item<=churn[d] :
if '.'in str(item):
dic[str(a)+"%~"+str(a+10)+"%"]=dic[str(a)+"%~"+str(a+10)+"%"]+1
print(i+' 正在运行')
#将每个项目的修改量的0~10%,10~20%……,每一段里面 总的软件缺陷数量 存入字典
labels=list(dic.keys())
sizes=list(dic.values())
mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.title('代码修改量和软件缺陷的关系')
plt.pie(sizes,labels=labels,autopct='%1.1f%%',shadow=False, textprops={'fontsize': 8, 'color': 'w'},startangle=90)
plt.axis('equal')
plt.legend(loc='lower right')
plt.show()
#将上面字典的键名作为labels,画图
六、结果展示