You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False
# 合并后的数据
before = {
'Total Lines': 12066,
'Source Code Lines': 8701,
'Comment Lines': 1638,
'Blank Lines': 1727,
'Source Code %': 72.1,
'Comment %': 13.6,
'Blank %': 14.3
}
after = {
'Total Lines': 23926,
'Source Code Lines': 16122,
'Comment Lines': 5145,
'Blank Lines': 2659,
'Source Code %': 67.4,
'Comment %': 21.5,
'Blank %': 11.1
}
# 设置新的配色方案
colors = {
'before': '#2E86AB', # 深蓝色
'after': '#F24236', # 红色
'pie': ['#2E86AB', '#F7B733', '#FC4A1A'] # 饼图配色
}
# 图1柱状图
fig, ax = plt.subplots(figsize=(12, 6))
categories = ['总行数', '代码行数', '注释行数', '空白行数']
before_values = [before['Total Lines'], before['Source Code Lines'],
before['Comment Lines'], before['Blank Lines']]
after_values = [after['Total Lines'], after['Source Code Lines'],
after['Comment Lines'], after['Blank Lines']]
x = np.arange(len(categories))
width = 0.35
# 创建柱状图
rects1 = ax.bar(x - width/2, before_values, width, label='维护前',
color=colors['before'], alpha=0.8)
rects2 = ax.bar(x + width/2, after_values, width, label='维护后',
color=colors['after'], alpha=0.8)
# 美化
ax.set_ylabel('行数')
ax.set_title('代码行数对比')
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend()
# 添加数值标签
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax.annotate(f'{int(height):,}',
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
autolabel(rects1)
autolabel(rects2)
plt.grid(True, axis='y', alpha=0.3)
plt.tight_layout()
plt.savefig('合并数据柱状图.png', dpi=300, bbox_inches='tight')
plt.close()
# 图2饼图对比
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
categories_percent = ['代码比例', '注释比例', '空白比例']
before_percent = [before['Source Code %'], before['Comment %'], before['Blank %']]
after_percent = [after['Source Code %'], after['Comment %'], after['Blank %']]
# 绘制饼图
def make_autopct(values):
def my_autopct(pct):
total = sum(values)
val = int(round(pct*total/100.0))
return f'{pct:.1f}%'
return my_autopct
ax1.pie(before_percent, labels=categories_percent, colors=colors['pie'],
autopct=make_autopct(before_percent), startangle=90)
ax1.set_title('维护前')
ax2.pie(after_percent, labels=categories_percent, colors=colors['pie'],
autopct=make_autopct(after_percent), startangle=90)
ax2.set_title('维护后')
plt.suptitle('代码构成比例对比')
plt.tight_layout()
plt.savefig('合并数据饼状图.png', dpi=300, bbox_inches='tight')
plt.close()