1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import pandas as pd import matplotlib.pyplot as plt import matplotlib.dates as mdates
data = { '任务名称': ['开发计划', '可行性分析报告', '业务分析与建模', '需求建模和用例分析', '架构设计和类设计', '数据库设计和物理视图设计', '总结'], '开始时间(天)': [1, 2, 3, 4, 5, 6, 7], '结束时间(天)': [2, 3, 4, 5, 6, 7, 8], '持续天数': [1, 1, 1, 1, 1, 1, 1], '责任人': ['夏浩祥'] * 7 }
df = pd.DataFrame(data)
origin_date = pd.Timestamp('2024-12-16') for col in ['开始时间(天)', '结束时间(天)']: try: df[col] = pd.to_datetime(df[col], unit='D', origin=origin_date) except ValueError as e: print(f"日期转换出现问题,列 {col} 的数据格式可能有误,错误信息:{e}")
fig, ax = plt.subplots(figsize=(10, 8))
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
for index, row in df.iterrows(): bar_width = (row['结束时间(天)'] - row['开始时间(天)']).days bar = ax.barh(row['任务名称'], width=bar_width, left=row['开始时间(天)'], height=0.8) text_x = row['开始时间(天)'] + pd.Timedelta(days=bar_width * 0.8) ax.text(text_x, row['任务名称'], row['责任人'], ha='left', va='center')
ax.xaxis_date() ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) ax.set_ylabel('任务名称') ax.set_xlabel('时间') ax.set_title('论坛管理系统开发甘特图')
plt.tight_layout()
plt.show()
|