first commit
This commit is contained in:
56
dynamic/plotter.py
Normal file
56
dynamic/plotter.py
Normal file
@ -0,0 +1,56 @@
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sns
|
||||
import sys
|
||||
|
||||
def create_plots(csv_file_path):
|
||||
"""
|
||||
读取CSV文件,生成图表并保存到磁盘
|
||||
"""
|
||||
try:
|
||||
df = pd.read_csv(csv_file_path)
|
||||
except FileNotFoundError:
|
||||
print(f"错误:找不到文件 {csv_file_path}")
|
||||
sys.exit(1)
|
||||
|
||||
# 算法名称映射
|
||||
algo_names = {
|
||||
'v1': '朴素动态规划(三层循环)',
|
||||
'v2': '优化二维动态规划',
|
||||
'v3': '空间优化一维动态规划'
|
||||
}
|
||||
df['algo_name'] = df['algo'].map(algo_names)
|
||||
|
||||
# 设置绘图风格
|
||||
sns.set_theme(style="whitegrid")
|
||||
|
||||
# 图表1:运行时间与物品数量的关系
|
||||
plt.figure(figsize=(10, 6))
|
||||
time_plot = sns.lineplot(data=df, x='n', y='time_us', hue='algo_name', marker='o', palette='viridis')
|
||||
plt.title('平均运行时间与物品种类数量(n)的关系')
|
||||
plt.xlabel('物品种类数量(n)')
|
||||
plt.ylabel('平均运行时间(微秒)')
|
||||
time_plot.legend(title='算法')
|
||||
plt.grid(True, which='both', linestyle='--')
|
||||
plt.savefig('time_vs_n.png', dpi=300)
|
||||
plt.close()
|
||||
|
||||
# 图表2:操作次数与物品数量的关系
|
||||
plt.figure(figsize=(10, 6))
|
||||
ops_plot = sns.lineplot(data=df, x='n', y='ops', hue='algo_name', marker='o', palette='plasma')
|
||||
plt.title('关键操作次数与物品种类数量(n)的关系')
|
||||
plt.xlabel('物品种类数量(n)')
|
||||
plt.ylabel('平均关键操作次数(对数尺度)')
|
||||
plt.yscale('log')
|
||||
ops_plot.legend(title='算法')
|
||||
plt.grid(True, which='both', linestyle='--')
|
||||
plt.savefig('ops_vs_n.png', dpi=300)
|
||||
plt.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
# 使用命令行参数指定的CSV文件
|
||||
create_plots(sys.argv[1])
|
||||
else:
|
||||
# 如果没有提供参数,使用默认文件名
|
||||
create_plots('results.csv')
|
||||
Reference in New Issue
Block a user